[comp.unix.programmer] Awk

ezra@daimi.aau.dk (Thomas Ravnholt) (03/07/91)

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.

Hope you can answer this one.

Thanks in advance !

                   ezra@daimi.aau.dk

mek@michael.udev.cdc.com (Mark Kennedy) (03/08/91)

In article <1991Mar7.115420.21315@daimi.aau.dk>, ezra@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
}'

You can use the pid if you are worried about stepping on duplicate
temp file names and insert a "trap" command to delete your temp
file(s) in case your script terminates prematurely.

     -Mark


-- 
                           ___  ___ 
                          / __||__ \ 
Mark Kennedy             ( |__  __| )  AT&T: (612) 482-2787
Control Data Corporation  \___||___/   E-Mail: mek@udev.cdc.com
			check-ins happen

tchrist@convex.COM (Tom Christiansen) (03/08/91)

From the keyboard of mek@michael.udev.cdc.com (Mark Kennedy):

: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
:}'
:

awk: syntax error near line 4
awk: illegal statement near line 4

can't do that stuff with vintage awk.

--tom
--
	I get so tired of utilities with arbitrary, undocumented,
	compiled-in limits.  Don't you?

Tom Christiansen		tchrist@convex.com	convex!tchrist

heiko@methan.chemie.fu-berlin.de (Heiko Schlichting) (03/08/91)

mek@michael.udev.cdc.com (Mark Kennedy) writes:
>ezra@daimi.aau.dk (Thomas Ravnholt) writes:

>> 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
>}'

Using GNU-awk 2.11.1 it is easier with:

awk 'BEGIN{ "date"|getline X; close("date"); print X}' /dev/null

Bye, Heiko.
-- 
 |~|    Heiko Schlichting                   | Freie Universitaet Berlin 
 / \    heiko@fub.uucp                      | Institut fuer Organische Chemie
/FUB\   heiko@methan.chemie.fu-berlin.de    | Takustrasse 3
`---'   phone +49 30 838-2677; fax ...-5163 | D-1000 Berlin 33  Germany

subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (03/08/91)

In article <8XTOBQT@methan.chemie.fu-berlin.de> admin@methan.chemie.fu-berlin.de writes:
>mek@michael.udev.cdc.com (Mark Kennedy) writes:
>>ezra@daimi.aau.dk (Thomas Ravnholt) writes:
>
>Using GNU-awk 2.11.1 it is easier with:
>
>awk 'BEGIN{ "date"|getline X; close("date"); print X}' /dev/null
>

Better yet, use perl :-)

perl -e '$d = `date`; print $d;'

				
						-Kartik


--
internet# find . -name core -exec cat {} \; |& tee /dev/tty*
subbarao@phoenix.Princeton.EDU -| Internet
kartik@silvertone.Princeton.EDU (NeXT mail)  
SUBBARAO@PUCC.BITNET			          - Bitnet

art@pilikia.pegasus.com (Art Neilson) (03/09/91)

In article <31137@shamash.cdc.com> mek@michael.udev.cdc.com (Mark Kennedy) writes:
>In article <1991Mar7.115420.21315@daimi.aau.dk>, ezra@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@pilikia.pegasus.com
Bank of Hawaii Tech Support	| UUCP: uunet!ucsd!nosc!pilikia!art