7.4. Closing Files and Pipes

If 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 opened 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 7.17.
(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

  1. Awk pipes each line from the input file to the UNIX sort utility.

  2. When the END block is reached, the pipe is closed. The string enclosed in double quotes must be identical to the pipe string where the pipe was initially opened.

The system ...

Get Linux Shells by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.