Go to the first, previous, next, last section, table of contents.


Closing Output Files and Pipes

When a file or pipe is opened, the file name or command associated with it is remembered by awk and subsequent writes to the same file or command are appended to the previous writes. The file or pipe stays open until awk exits. This is usually convenient.

Sometimes there is a reason to close an output file or pipe earlier than that. To do this, use the close function, as follows:

close(filename)

or

close(command)

The argument filename or command can be any expression. Its value must exactly equal the string used to open the file or pipe to begin with--for example, if you open a pipe with this:

print $1 | "sort -r > names.sorted"

then you must close it with this:

close("sort -r > names.sorted")

Here are some reasons why you might need to close an output file:

close returns a value of zero if the close succeeded. Otherwise, the value will be non-zero.


Go to the first, previous, next, last section, table of contents.