Execute command within AWK

  • Follow


Hi,
Is it possible for me to invoke other commands/scripts from gawk. For
example can I invoke "grep" or "wc" from within awk like

gawk '{grep ....}' file 1

"grep" and "wc" are just mentioned as an example. But the question is
about invoking commands/scripts from awk.

Thanks,
Prateek

0
Reply prateek.a (24) 6/6/2007 7:36:14 PM

Prateek wrote:
> Hi,
> Is it possible for me to invoke other commands/scripts from gawk. For
> example can I invoke "grep" or "wc" from within awk like
> 
> gawk '{grep ....}' file 1
> 
> "grep" and "wc" are just mentioned as an example. But the question is
> about invoking commands/scripts from awk.
> 
> Thanks,
> Prateek
> 

Yes:

$ awk 'BEGIN{system("echo hello")}'
hello
$ awk 'BEGIN{print "echo hello" | "/bin/sh" }'
hello

It's usually a sign that you're very confused about the best way to 
design your script though....

	Ed.
0
Reply Ed 6/6/2007 7:42:18 PM


On Wed, 06 Jun 2007 12:36:14 -0700, Prateek <prateek.a@gmail.com>
wrote:

>Hi,
>Is it possible for me to invoke other commands/scripts from gawk. For
>example can I invoke "grep" or "wc" from within awk like
>
>gawk '{grep ....}' file 1
>
>"grep" and "wc" are just mentioned as an example. But the question is
>about invoking commands/scripts from awk.
>

grep is a poor example - wc is only somewhat less so because of the
large overlap in functionality.

However, wget is a good example - this is composite code from several
scripts:

  	WgetCommand = "wget   -t 3 -T 30 -O- -U \"Mozilla/5.0
(Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219
Firefox/2.0.0.2\" http://www.wire-heads.com/istrip/ 2>"

  	StdErrFile = Array[ 4 ] ".response"
	WgetCommand = WgetCommand StdErrFile
	while( ( WgetCommand | getline ) > 0 ) {
		if( !x ) {
			if( match( tolower( $0 ),
/comic?[sx]?[0-9]?[\/=]([a-z0-9\-_\/]+)\./, Arr ) ) x = Arr[1]
		}

(Actually, that is not working code because that test doesn't go with
that URL.)


-- 
T.E.D. (tdavis@gearbox.maem.umr.edu)
Remove "gearbox.maem." from address - that one is dead
0
Reply Ted 6/6/2007 8:12:40 PM

Ed Morton wrote:
> Prateek wrote:
>> Hi,
>> Is it possible for me to invoke other commands/scripts from gawk. For
>> example can I invoke "grep" or "wc" from within awk like
>>
>> gawk '{grep ....}' file 1
>>
>> "grep" and "wc" are just mentioned as an example. But the question is
>> about invoking commands/scripts from awk.
>>
>> Thanks,
>> Prateek
>>
> 
> Yes:
> 
> $ awk 'BEGIN{system("echo hello")}'
> hello
> $ awk 'BEGIN{print "echo hello" | "/bin/sh" }'
> hello
> 
> It's usually a sign that you're very confused about the best way to 
> design your script though....
> 
>     Ed.

Just one note, for Prateek, not all implementations of awk have the "system" 
command. Get GNU-awk.


-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GE d+(-) s+: a@ C+ ULAHS++$ P- L+>++ E--- W++ N++ o !K w--(+) O- M?>+ V? PS+
PE+(++) Y+ PGP- t+ 5 X R !tv b+ DI(+) D G e++ h---- r+++@ y++++
------END GEEK CODE BLOCK------
0
Reply SiKing 6/11/2007 10:07:10 AM

3 Replies
280 Views

(page loaded in 0.064 seconds)

Similiar Articles:













7/21/2012 1:18:19 AM


Reply: