[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.8.6 Using getline into a Variable from a Pipe

When you use `command | getline var', the output of command is sent through a pipe to getline and into the variable var. For example, the following program reads the current date and time into the variable current_time, using the date utility, and then prints it:

 
BEGIN {
     "date" | getline current_time
     close("date")
     print "Report printed on " current_time
}

In this version of getline, none of the built-in variables are changed and the record is not split into fields.

According to POSIX, `expression | getline var' is ambiguous if expression contains unparenthesized operators other than `$'; for example, `"echo " "date" | getline var' is ambiguous because the concatenation operator is not parenthesized. You should write it as `("echo " "date") | getline var' if you want your program to be portable to other awk implementations. (It happens that gawk gets it right, but you should not rely on this. Parentheses make it easier to read, anyway.)



This document was generated on May 2, 2002 using texi2html