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.

114 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>ENVIRONERRNO #name. When gawk is processing the input files, ‘FILENAME == ARGV[ARGIND]’is always true.This variable is useful in file processing; it allows you to tell how far along youare in the list of data files as well as to distinguish between successive instancesof the same file name on the command line.While you can change the value of ARGIND within your awk program, gawkautomatically sets it to a new value when the next file is opened.This variable is a gawk extension. In other awk implementations, or if gawk isin compatibility mode (see Section 11.2 [Command-Line Options], page 177),it is not special.An associative array that contains the values of the environment. The arrayindices are the environment variable names; the elements are the values of theparticular environment variables. For example, ENVIRON["HOME"] might be‘/home/arnold’. Changing this array does not affect the environment passedon to any programs that awk may spawn via redirection or the system function.Some operating systems may not have environment variables. On such systems,the ENVIRON array is empty (except for ENVIRON["<strong>AWK</strong>PATH"], see Section 11.4[The <strong>AWK</strong>PATH Environment Variable], page 183).If a system error occurs during a redirection for getline, during a read forgetline, or during a close operation, then ERRNO contains a string describingthe error.ERRNO works similarly to the C variable errno. In particular gawk never clearsit (sets it to zero or ""). Thus, you should only expect its value to be meaningfulwhen an I/O operation returns a failure value, such as getline returning −1.You are, of course, free to clear it yourself before doing an I/O operation.This variable is a gawk extension. In other awk implementations, or if gawk isin compatibility mode (see Section 11.2 [Command-Line Options], page 177),it is not special.FILENAME The name of the file that awk is currently reading. When no data files arelisted on the command line, awk reads from the standard input and FILENAMEis set to "-". FILENAME is changed each time a new file is read (see Chapter 3[Reading Input Files], page 36). Inside a BEGIN rule, the value of FILENAME is"", since there are no input files being processed yet. 3 Note, though, thatusing getline (see Section 3.8 [Explicit Input with getline], page 52) insidea BEGIN rule can give FILENAME a value.FNRNFThe current record number in the current file. FNR is incremented each time anew record is read (see Section 3.8 [Explicit Input with getline], page 52). Itis reinitialized to zero each time a new input file is started.The number of fields in the current input record. NF is set each time a newrecord is read, when a new field is created or when $0 changes (see Section 3.2[Examining Fields], page 39).3 Some early implementations of Unix awk initialized FILENAME to "-", even if there were data files to beprocessed. This behavior was incorrect and should not be relied upon in your programs.

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

Saved successfully!

Ooh no, something went wrong!