Next: , Previous: , Up: Getline   [Contents][Index]


4.9.7 Using getline from a Coprocess

Reading 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 to 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 the 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 and RT.

Coprocesses are an advanced feature. They are discussed here only because this is the section on getline. See Two-way I/O, where coprocesses are discussed in more detail.