(gawk.info.gz) BEGINFILE/ENDFILE
Info Catalog
(gawk.info.gz) BEGIN/END
(gawk.info.gz) Pattern Overview
(gawk.info.gz) Empty
7.1.5 The `BEGINFILE' and `ENDFILE' Special Patterns
----------------------------------------------------
This minor node describes a `gawk'-specific feature.
Two special kinds of rule, `BEGINFILE' and `ENDFILE', give you
"hooks" into `gawk''s command-line file processing loop. As with the
`BEGIN' and `END' rules ( BEGIN/END), all `BEGINFILE' rules in a
program are merged, in the order they are read by `gawk', and all
`ENDFILE' rules are merged as well.
The body of the `BEGINFILE' rules is executed just before `gawk'
reads the first record from a file. `FILENAME' is set to the name of
the current file, and `FNR' is set to zero.
The `BEGINFILE' rule provides you the opportunity for two tasks that
would otherwise be difficult or impossible to perform:
* You can test if the file is readable. Normally, it is a fatal
error if a file named on the command line cannot be opened for
reading. However, you can bypass the fatal error and move on to
the next file on the command line.
You do this by checking if the `ERRNO' variable is not the empty
string; if so, then `gawk' was not able to open the file. In this
case, your program can execute the `nextfile' statement (
Nextfile Statement). This causes `gawk' to skip the file
entirely. Otherwise, `gawk' exits with the usual fatal error.
* If you have written extensions that modify the record handling (by
inserting an "open hook"), you can invoke them at this point,
before `gawk' has started processing the file. (This is a _very_
advanced feature, currently used only by the XMLgawk project
(http://xmlgawk.sourceforge.net).)
The `ENDFILE' rule is called when `gawk' has finished processing the
last record in an input file. For the last input file, it will be
called before any `END' rules. The `ENDFILE' rule is executed even for
empty input files.
Normally, when an error occurs when reading input in the normal input
processing loop, the error is fatal. However, if an `ENDFILE' rule is
present, the error becomes non-fatal, and instead `ERRNO' is set. This
makes it possible to catch and process I/O errors at the level of the
`awk' program.
The `next' statement ( Next Statement) is not allowed inside
either a `BEGINFILE' or and `ENDFILE' rule. The `nextfile' statement
( Nextfile Statement) is allowed only inside a `BEGINFILE' rule,
but not inside an `ENDFILE' rule.
The `getline' statement ( Getline) is restricted inside both
`BEGINFILE' and `ENDFILE'. Only the `getline VARIABLE < FILE' form is
allowed.
`BEGINFILE' and `ENDFILE' are `gawk' extensions. In most other
`awk' implementations, or if `gawk' is in compatibility mode (
Options), they are not special.
Info Catalog
(gawk.info.gz) BEGIN/END
(gawk.info.gz) Pattern Overview
(gawk.info.gz) Empty
automatically generated by
info2html