If the same file name or the same shell command is used with
getline
(see section Explicit Input with getline
)
more than once during the execution of an awk
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 getline
, another
record is read from it, and so on.
Similarly, when a file or pipe is opened for output, 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 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:
close(filename)
or
close(command)
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 print
or 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 awk
programs.
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
.
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.
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
use close
on your files when you are done with them.
Go to the first, previous, next, last section, table of contents.