- 论坛徽章:
- 145
|
回复 5# reyleon
1. thank you for your information with close(cmd)
2. getline please refer manual ...
Note: I think using "|" can do it well in this case
http://www.gnu.org/software/gawk/manual/gawk.html#Getline_002fCoprocess
4.9.10 Summary of getline Variants
Table 4.1 summarizes the eight variants of getline, listing which built-in variables are set by each one, and whether the variant is standard or a gawk extension. Note: for each variant, gawk sets the RT built-in variable.
Variant Effect Standard / Extension
getline Sets $0, NF, FNR, NR, and RT Standard
getline var Sets var, FNR, NR, and RT Standard
getline < file Sets $0, NF, and RT Standard
getline var < file Sets var and RT Standard
command | getline Sets $0, NF, and RT Standard
command | getline var Sets var and RT Standard
command |& getline Sets $0, NF, and RT Extension
command |& getline var Sets var and RT Extension
...
4.9.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 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 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.
...
4.9.8 Using getline into a Variable from a Coprocess
When you use ‘command |& getline var’, the output from the coprocess command is sent through a two-way pipe to getline and into the variable var.
In this version of getline, none of the built-in variables are changed and the record is not split into fields. The only variable changed is var. However, RT is set. |
|