www.delorie.com/gnu/docs/gawk/gawk_52.html   search  
Buy the book!

The GNU Awk User's Guide

[ < ] [ > ]   [ << ] [ 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:

     "date" | getline current_time
     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.)

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003