is similar to the
However, instead of abandoning processing of the current record, the
nextfile statement instructs
awk to stop processing the
current data file.
Upon execution of the
updated to the name of the next data file listed on the command line,
FNR is reset to one,
starts over with the first rule in the program.
nextfile statement causes the end of the input to be reached,
then the code in any
END rules is executed. An exception to this is
nextfile is invoked during execution of any statement in an
END rule; In this case, it causes the program to stop immediately. See BEGIN/END.
nextfile statement is useful when there are many data files
to process but it isn’t necessary to process every record in every file.
in order to move on to the next data file, a program
would have to continue scanning the unwanted records. The
statement accomplishes this much more efficiently.
gawk, execution of
nextfile causes additional things
ENDFILE rules are executed except in the case as
ARGIND is incremented,
BEGINFILE rules are executed.
ARGIND hasn’t been introduced yet. See Built-in Variables.)
nextfile is useful inside a
rule to skip over a file that would otherwise cause
to exit with a fatal error. In this case,
ENDFILE rules are not
executed. See BEGINFILE/ENDFILE.
While one might think that ‘close(FILENAME)’ would accomplish
the same as
nextfile, this isn’t true.
reserved for closing files, pipes, and coprocesses that are
opened with redirections. It is not related to the main processing that
awk does with the files listed in
NOTE: For many years,
gawkextension. As of September, 2012, it was accepted for inclusion into the POSIX standard. See the Austin Group website.
The current version of the Brian Kernighan’s
mawk (see Other Versions) also support
nextfile. However, they don’t allow the
nextfile statement inside function bodies (see User-defined).
gawk does; a
nextfile inside a function body reads the
next record and starts processing it with the first rule in the program,
just as any other