Go to the first, previous, next, last section, table of contents.
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:
awk
program. Close the file when you are finished writing it; then
you can start reading it with getline
(see section Explicit Input with getline).
awk
program. If you don't close the files, eventually you may exceed a
system limit on the number of open files in one process. So close
each one when you are finished writing it.
mail program, the message is not
actually sent until the pipe is closed.
mail program. If you
output several lines redirected to this pipe without closing it, they make
a single message of several lines. By contrast, if you close the pipe
after each line of output, then each line makes a separate message.
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.