21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6: Patterns, Actions, and Variables 109next statement inside a function body reads the next record and starts processing it withthe first rule in the program. If the next statement causes the end of the input to bereached, then the code in any END rules is executed. See Section 6.1.4 [The BEGIN and ENDSpecial Patterns], page 99.6.4.9 Using gawk’s nextfile Statementgawk provides the nextfile statement, which is similar to the next statement. However,instead of abandoning processing of the current record, the nextfile statement instructsgawk to stop processing the current data file.The nextfile statement is a gawk extension. In most other awk implementations, orif gawk is in compatibility mode (see Section 11.2 [Command-Line Options], page 177),nextfile is not special.Upon execution of the nextfile statement, FILENAME is updated to the name of thenext data file listed on the command line, FNR is reset to one, ARGIND is incremented, andprocessing starts over with the first rule in the program. (ARGIND hasn’t been introducedyet. See Section 6.5 [Built-in Variables], page 110.) If the nextfile statement causes theend of the input to be reached, then the code in any END rules is executed. See Section 6.1.4[The BEGIN and END Special Patterns], page 99.The nextfile statement is useful when there are many data files to process but itisn’t necessary to process every record in every file. Normally, in order to move on to thenext data file, a program has to continue scanning the unwanted records. The nextfilestatement 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 openedwith redirections. It is not related to the main processing that awk does with the files listedin ARGV.If it’s necessary to use an awk version that doesn’t support nextfile, see Section 12.2.1[Implementing nextfile as a Function], page 188, for a user-defined function that simulatesthe nextfile statement.The current version of the Bell Laboratories awk (see Section B.6 [Other Freely Availableawk Implementations], page 281) also supports nextfile. However, it doesn’t allowthe nextfile statement inside function bodies (see Section 8.2 [User-Defined Functions],page 153). gawk does; a nextfile inside a function body reads the next record and startsprocessing 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 nextfilestatement. 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 wasa regular identifier. The old usage is no longer accepted; ‘next file’ generates a syntaxerror.6.4.10 The exit StatementThe exit statement causes awk to immediately stop executing the current rule and to stopprocessing input; any remaining input is ignored. The exit statement is written as follows:exit [return code]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!