21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

188 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>12.2 General <strong>Programming</strong>This section presents a number of functions that are of general programming use.12.2.1 Implementing nextfile as a FunctionThe nextfile statement, presented in Section 6.4.9 [Using gawk’s nextfile Statement],page 109, is a gawk-specific extension—it is not available in most other implementations ofawk. This section shows two versions of a nextfile function that you can use to simulategawk’s nextfile statement if you cannot use gawk.A first attempt at writing a nextfile function is as follows:# nextfile --- skip remaining records in current file# this should be read in before the "main" awk programfunction nextfile() { _abandon_ = FILENAME; next }_abandon_ == FILENAME { next }Because it supplies a rule that must be executed first, this file should be included beforethe main program. This rule compares the current data file’s name (which is always in theFILENAME variable) to a private variable named _abandon_. If the file name matches, thenthe action part of the rule executes a next statement to go on to the next record. (Theuse of ‘_’ in the variable name is a convention. It is discussed more fully in Section 12.1[Naming Library Function Global Variables], page 186.)The use of the next statement effectively creates a loop that reads all the records fromthe current data file. The end of the file is eventually reached and a new data file isopened, changing the value of FILENAME. Once this happens, the comparison of _abandon_to FILENAME fails, and execution continues with the first rule of the “real” program.The nextfile function itself simply sets the value of _abandon_ and then executes anext statement to start the loop.This initial version has a subtle problem. If the same data file is listed twice on thecommand line, one right after the other or even with just a variable assignment betweenthem, this code skips right through the file a second time, even though it should stop whenit gets to the end of the first occurrence. A second version of nextfile that remedies thisproblem is shown here:# nextfile --- skip remaining records in current file# correctly handle successive occurrences of the same file# this should be read in before the "main" awk programfunction nextfile() { _abandon_ = FILENAME; next }_abandon_ == FILENAME {if (FNR == 1)_abandon_ = ""elsenext}The nextfile function has not changed. It makes _abandon_ equal to the current filename and then executes a next statement. The next statement reads the next record and

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

Saved successfully!

Ooh no, something went wrong!