Awk

Art Neilson art at pilikia.pegasus.com
Sat Mar 9 04:25:18 AEST 1991


In article <31137 at shamash.cdc.com> mek at michael.udev.cdc.com (Mark Kennedy) writes:
>In article <1991Mar7.115420.21315 at daimi.aau.dk>, ezra at daimi.aau.dk (Thomas Ravnholt) writes:
>|> Hello !
>|> 
>|>   I have a little question about awk (nawk).
>|> 
>|>   If I want to run a unix-command in an awk-script,
>|> how do I get the output into a variable.
>|> 
>|>   I tried
>|> 
>|>      getline < system(unixcommand)
>|> 
>|>      system(unixcommand | getline)
>|> 
>|> 
>|> but it is no good of course. system returns 0 or 1 and
>|> not the output of the unixcommand.
>|> 
>
>Crude, but effective, temporary files are your friend.  Here's a
>simple example that does what you ask.
>
>#! /bin/sh
>
>awk '
>BEGIN {
>system("date > /tmp/foo")
>getline X < "/tmp/foo"
>print X
>}'

Since the original poster did ask about nawk as well, here's the
solution to his problem in nawk:

#! /usr/bin/nawk -f
BEGIN {
	"/bin/date" | getline d
	print d
	exit
}
-- 
Arthur W. Neilson III		| INET: art at pilikia.pegasus.com
Bank of Hawaii Tech Support	| UUCP: uunet!ucsd!nosc!pilikia!art



More information about the Comp.unix.programmer mailing list