If the same file name or the same shell command is used with
(see section Explicit Input with
more than once during the execution of an
program, the file is opened (or the command is executed) only the first time.
At that time, the first record of input is read from that file or command.
The next time the same file or command is used in
record is read from it, and so on.
Similarly, when a file or pipe is opened for output, the file name or command
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
This implies that if you want to start reading the same file again from
the beginning, or if you want to rerun a shell command (rather than
reading more output from the command), you must take special steps.
What you must do is use the
close function, as follows:
The argument filename or command can be any expression. Its value must exactly match the string that was used to open the file or start the command (spaces and other "irrelevant" characters included). For example, if you open a pipe with this:
"sort -r names" | getline foo
then you must close it with this:
close("sort -r names")
Once this function call is executed, the next
getline from that
file or command, or the next
printf to that
file or command, will reopen the file or rerun the command.
Because the expression that you use to close a file or pipeline must exactly match the expression used to open the file or run the command, it is good practice to use a variable to store the file name or command. The previous example would become
sortcom = "sort -r names" sortcom | getline foo ... close(sortcom)
This helps avoid hard-to-find typographical errors in your
Here are some reasons why you might need to close an output file:
awkprogram. Close the file when you are finished writing it; then you can start reading it with
awkprogram. 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.
close returns a value of zero if the close succeeded.
Otherwise, the value will be non-zero.
In this case,
gawk sets the variable
ERRNO to a string
describing the error that occurred.
If you use more files than the system allows you to have open,
gawk will attempt to multiplex the available open files among
your data files.
gawk's ability to do this depends upon the
facilities of your operating system: it may not always work. It is
therefore both good practice and good portability advice to always
close on your files when you are done with them.
Go to the first, previous, next, last section, table of contents.