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.

134 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>$ nawk ’BEGIN { print length(x) ; x[1] = 1 }’⊣ 0If ‘--lint’ has been specified on the command line, gawk issues a warning aboutthis.Beginning with gawk version 3.1.5, when supplied an array argument, thelength function returns the number of elements in the array. This is less usefulthan it might seem at first, as the array is not guaranteed to be indexed fromone to the number of elements in it. If ‘--lint’ is provided on the commandline (see Section 11.2 [Command-Line Options], page 177), gawk warns thatpassing an array argument is not portable. If ‘--posix’ is supplied, using anarray argument is a fatal error (see Chapter 7 [Arrays in awk], page 119).match(string, regexp [, array])The match function searches string for the longest, leftmost substring matchedby the regular expression, regexp. It returns the character position, or index,at which that substring begins (one, if it starts at the beginning of string). Ifno match is found, it returns zero.The regexp argument may be either a regexp constant (‘/.../’) or a stringconstant (". . ."). In the latter case, the string is treated as a regexp to bematched. Section 2.8 [Using Dynamic Regexps], page 34, for a discussion of thedifference between the two forms, and the implications for writing your programcorrectly.The order of the first two arguments is backwards from most other string functionsthat work with regular expressions, such as sub and gsub. It might help toremember that for match, the order is the same as for the ‘~’ operator: ‘string~ regexp’.The match function sets the built-in variable RSTART to the index. It alsosets the built-in variable RLENGTH to the length in characters of the matchedsubstring. If no match is found, RSTART is set to zero, and RLENGTH to −1.For example:{if ($1 == "FIND")regex = $2else {where = match($0, regex)if (where != 0)print "Match of", regex, "found at",where, "in", $0}}This program looks for lines that match the regular expression stored in thevariable regex. This regular expression can be changed. If the first word on aline is ‘FIND’, regex is changed to be the second word on that line. Therefore,if given:FIND ru+nMy program runs

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

Saved successfully!

Ooh no, something went wrong!