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