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

The GNU Awk User's Guide

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

4.8.7 Using getline from a Coprocess

Input into getline from a pipe is a one-way operation. The command that is started with `command | getline' only sends data to your awk program.

On occasion, you might want to send data to another program for processing and then read the results back. gawk allows you start a coprocess, with which two-way communications are possible. This is done with the `|&' operator. Typically, you write data to the coprocess first and then read results back, as shown in the following:

print "some query" |& "db_server"
"db_server" |& getline

which sends a query to db_server and then reads the results.

The values of NR and FNR are not changed, because the main input stream is not used. However, the record is split into fields in the normal manner, thus changing the values of $0, of the other fields, and of NF.

Coprocesses are an advanced feature. They are discussed here only because this is the section on getline. See section Two-Way Communications with Another Process, where coprocesses are discussed in more detail.

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