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

7.1.5 The BEGINFILE and ENDFILE Special Patterns

This section 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 (see section The BEGIN and END Special Patterns), 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:

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 (see section The next Statement) is not allowed inside either a BEGINFILE or and ENDFILE rule. The nextfile statement (see section Using gawk’s nextfile Statement) is allowed only inside a BEGINFILE rule, but not inside an ENDFILE rule.

The getline statement (see section Explicit Input with 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 (see section Command-Line Options), they are not special.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on March 30, 2012 using texi2html 5.0.