(gawk.info.gz) Nextfile Statement
Info Catalog
(gawk.info.gz) Next Statement
(gawk.info.gz) Statements
(gawk.info.gz) Exit Statement
Using `gawk''s `nextfile' Statement
-----------------------------------
`gawk' provides the `nextfile' statement, which is similar to the
`next' statement. 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 (
Command-Line Options Options.), `nextfile' is not special.
Upon execution of the `nextfile' statement, `FILENAME' is updated to
the name of the next data file listed on the command line, `FNR' is
reset to one, `ARGIND' is incremented, and processing starts over with
the first rule in the program. (`ARGIND' hasn't been introduced yet.
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. The `BEGIN' and `END' Special Patterns BEGIN/END.
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.
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'.
If it's necessary to use an `awk' version that doesn't support
`nextfile', see Implementing `nextfile' as a Function Nextfile
Function, for a user-defined function that simulates the `nextfile'
statement.
The current version of the Bell Laboratories `awk' ( Other
Freely Available `awk' Implementations Other Versions.) also supports
`nextfile'. However, it doesn't allow the `nextfile' statement inside
function bodies ( User-Defined Functions 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 `nextfile' statement.
*Caution:* Versions of `gawk' prior to 3.0 used two words (`next
file') for the `nextfile' statement. In version 3.0, this was changed
to one word, because the treatment of `file' was inconsistent. When it
appeared after `next', `file' was a keyword; otherwise, it was a
regular identifier. The old usage is no longer accepted; `next file'
generates a syntax error.
Info Catalog
(gawk.info.gz) Next Statement
(gawk.info.gz) Statements
(gawk.info.gz) Exit Statement
automatically generated byinfo2html