This section describes a gawk-specific feature.
Two special kinds of rule,
you “hooks” into gawk's command-line file processing loop.
As with the
END rules (see 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.
is set to the name of the current file, and
FNR is set to zero.
BEGINFILE rule provides you the opportunity to accomplish two tasks
that would otherwise be difficult or impossible to perform:
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
(see Nextfile Statement). This causes gawk to skip
the file entirely. Otherwise, gawk exits with the usual
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
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
rule is present, the error becomes non-fatal, and instead
is set. This makes it possible to catch and process I/O errors at the
level of the awk program.
next statement (see Next Statement) is not allowed inside
BEGINFILE or and
ENDFILE rule. The
statement (see Nextfile Statement) is allowed only inside a
BEGINFILE rule, but not inside an
getline statement (see Getline) is restricted inside
ENDFILE. Only the ‘getline
variable < file’ form is allowed.
ENDFILE are gawk extensions.
In most other awk implementations, or if gawk is in
compatibility mode (see Options), they are not special.