manpagez: man pages & more
info gawk
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.4.9 Using gawk’s nextfile Statement

gawk provides the nextfile statement, which is similar to the next statement. (c.e.) However, instead of abandoning processing of the current record, the nextfile statement instructs gawk to stop processing the current data file.

The nextfile statement is a gawk extension. In most other awk implementations, or if gawk is in compatibility mode (see section Command-Line Options), nextfile is not special.

Upon execution of the nextfile statement, any ENDFILE rules are executed, FILENAME is updated to the name of the next data file listed on the command line, FNR is reset to one, ARGIND is incremented, any BEGINFILE rules are executed, and processing starts over with the first rule in the program. (ARGIND hasn’t been introduced yet. See section Built-in Variables.) If the nextfile statement causes the end of the input to be reached, then the code in any END rules is executed. See section The BEGIN and END Special Patterns.

The nextfile statement is useful when there are many data files to process but it isn’t necessary to process every record in every file. Normally, in order to move on to the next data file, a program has to continue scanning the unwanted records. The nextfile statement accomplishes this much more efficiently.

In addition, nextfile is useful inside a BEGINFILE rule to skip over a file that would otherwise cause gawk to exit with a fatal error. See section The BEGINFILE and ENDFILE Special Patterns.

While one might think that ‘close(FILENAME)’ would accomplish the same as nextfile, this isn’t true. close() is 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 ARGV.

The current version of the Brian Kernighan’s awk (see section Other Freely Available awk Implementations) also supports nextfile. However, it doesn’t allow the nextfile statement inside function bodies (see section User-Defined Functions). 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 nextfile statement.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2024
Individual documents may contain additional copyright information.