DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(gawk.info) Getline/Pipe

Info Catalog (gawk.info) Getline/Variable/File (gawk.info) Getline (gawk.info) Getline/Variable/Pipe
 
 Using `getline' from a Pipe
 ---------------------------
 
    You can pipe the output of a command into `getline', using `COMMAND
 | getline'.  In this case, the string COMMAND is run as a shell command
 and its output is piped into `awk' to be used as input.  This form of
 `getline' reads one record at a time from the pipe.
 
    For example, the following program copies its input to its output,
 except for lines that begin with `@execute', which are replaced by the
 output produced by running the rest of the line as a shell command:
 
      awk '{
           if ($1 == "@execute") {
                tmp = substr($0, 10)
                while ((tmp | getline) > 0)
                     print
                close(tmp)
           } else
                print
      }'
 
 The `close' function is called to ensure that if two identical
 `@execute' lines appear in the input, the command is run for each one.
  Closing Input and Output Files and Pipes Close Files And Pipes.
 
    Given the input:
 
      foo
      bar
      baz
      @execute who
      bletch
 
 the program might produce:
 
      foo
      bar
      baz
      arnold     ttyv0   Jul 13 14:22
      miriam     ttyp0   Jul 13 14:23     (murphy:0)
      bill       ttyp1   Jul 13 14:23     (murphy:0)
      bletch
 
 Notice that this program ran the command `who' and printed the result.
 (If you try this program yourself, you will of course get different
 results, showing you who is logged in on your system.)
 
    This variation of `getline' splits the record into fields, sets the
 value of `NF' and recomputes the value of `$0'.  The values of `NR' and
 `FNR' are not changed.
 
    According to POSIX, `EXPRESSION | getline' is ambiguous if
 EXPRESSION contains unparenthesized operators other than `$'; for
 example, `"echo " "date" | getline' is ambiguous because the
 concatenation operator is not parenthesized, and you should write it as
 `("echo " "date") | getline' 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.)
 
Info Catalog (gawk.info) Getline/Variable/File (gawk.info) Getline (gawk.info) Getline/Variable/Pipe
automatically generated byinfo2html