< Day Day Up > |
6.15. PipesIf you open a pipe in an awk program, you must close it before opening another one. The command on the right-hand side of the pipe symbol is enclosed in double quotes. Only one pipe can be open at a time. Example 6.102.(The Database) % cat names john smith alice cheba george goldberg susan goldberg tony tram barbara nguyen elizabeth lone dan savage eliza goldberg john goldenrod (The Command Line) % nawk '{print $1, $2 | "sort –r +1 –2 +0 –1 "}' names (The Output) tony tram john smith dan savage barbara nguyen elizabeth lone john goldenrod susan goldberg george goldberg eliza goldberg alice cheba EXPLANATION Awk will pipe the output of the print statement as input to the UNIX/Linux sort command, which does a reversed sort using the second field as the primary key and the first field as the secondary key (i.e., sort by last name in reverse). The UNIX/Linux command must be enclosed in double quotes. (See "sort" in Appendix A.) 6.15.1 Closing Files and PipesIf you plan to use a file or pipe in an awk program again for reading or writing, you may want to close it first, because it remains open until the script ends. Once opened, the pipe remains open until awk exits. Therefore, statements in the END block will also be affected by the pipe. The first line in the END block closes the pipe. Example 6.103.(In Script) 1 { print $1, $2, $3 | " sort -r +1 -2 +0 -1"} END{ 2 close("sort –r +1 –2 +0 –1") <rest of statements> } EXPLANATION
The system FunctionThe built-in system function takes a UNIX/Linux system command as its argument, executes the command, and returns the exit status to the awk program. It is similar to the C standard library function, also called system( ). The UNIX/Linux command must be enclosed in double quotes. FORMAT system( "UNIX/Linux Command") Example 6.104.(In Script) { 1 system ( "cat" $1 ) 2 system ( "clear" ) } EXPLANATION
|
< Day Day Up > |