Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!math.fu-berlin.de!fub!heiko From: heiko@methan.chemie.fu-berlin.de (Heiko Schlichting) Newsgroups: comp.unix.programmer Subject: Re: Awk Message-ID: <8XTOBQT@methan.chemie.fu-berlin.de> Date: 8 Mar 91 00:41:55 GMT References: <1991Mar7.115420.21315@daimi.aau.dk> <31137@shamash.cdc.com> Reply-To: admin@methan.chemie.fu-berlin.de Organization: Free University of Berlin, Organic Chemistry Lines: 35 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