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 3: Reading Input Files 57• An interesting side effect occurs if you use getline without a redirection inside a BEGINrule. Because an unredirected getline reads from the command-line data files, the firstgetline command causes awk to set the value of FILENAME. Normally, FILENAME doesnot have a value inside BEGIN rules, because you have not yet started to process thecommand-line data files. (See Section 6.1.4 [The BEGIN and END Special Patterns],page 99, also see Section 6.5.2 [Built-in Variables That Convey Information], page 113.)• Using FILENAME with getline (‘getline < FILENAME’) is likely to be a source for confusion.awk opens a separate input stream from the current input file. However, bynot using a variable, $0 and NR are still updated. If you’re doing this, it’s probably byaccident, and you should reconsider what it is you’re trying to accomplish.3.8.10 Summary of getline VariantsTable 3.1 summarizes the eight variants of getline, listing which built-in variables are setby each one.VariantEffectgetlineSets $0, NF, FNR, and NRgetline varSets var, FNR, and NRgetline < fileSets $0 and NFgetline var < fileSets varcommand | getlineSets $0 and NFcommand | getline var Sets varcommand |& getlineSets $0 and NF. This is a gawk extensioncommand |& getline var Sets var. This is a gawk extensionTable 3.1: getline Variants and What They Set

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

Saved successfully!

Ooh no, something went wrong!