Go backward to Getline.
Go up to Reading Files.
Closing Input Files and Pipes
If the same file name or the same shell command is used 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.
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 equal the string that was used to open the file or start
the command--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 will reopen the file or rerun the command.
`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.