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.

116 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>RSTARTRT #The start-index in characters of the substring that is matched by the matchfunction (see Section 8.1.3 [String-Manipulation Functions], page 132). RSTARTis set by invoking the match function. Its value is the position of the stringwhere the matched substring starts, or zero if no match was found.This is set each time a record is read. It contains the input text that matchedthe text denoted by RS, the record separator.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.Advanced Notes: Changing NR and FNRawk increments NR and FNR each time it reads a record, instead of setting them to theabsolute value of the number of records read. This means that a program can change thesevariables and their new values are incremented for each record. This is demonstrated inthe following example:$ echo ’1> 2> 3> 4’ | awk ’NR == 2 { NR = 17 }> { print NR }’⊣ 1⊣ 17⊣ 18⊣ 19Before FNR was added to the awk language (see Section A.1 [Major Changes Between V7 andSVR3.1], page 257), many awk programs used this feature to track the number of recordsin a file by resetting NR to zero when FILENAME changed.6.5.3 Using ARGC and ARGVSection 6.5.2 [Built-in Variables That Convey Information], page 113, presented the followingprogram describing the information contained in ARGC and ARGV:$ awk ’BEGIN {> for (i = 0; i < ARGC; i++)> print ARGV[i]> }’ inventory-shipped BBS-list⊣ awk⊣ inventory-shipped⊣ BBS-listIn this example, ARGV[0] contains ‘awk’, ARGV[1] contains ‘inventory-shipped’, andARGV[2] contains ‘BBS-list’. Notice that the awk program is not entered in ARGV. Theother special command-line options, with their arguments, are also not entered. This includesvariable assignments done with the ‘-v’ option (see Section 11.2 [Command-LineOptions], page 177). Normal variable assignments on the command line are treated asarguments and do show up in the ARGV array:$ cat showargs.awk

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

Saved successfully!

Ooh no, something went wrong!