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.

Chapter 8: Functions 137Note also that strtonum uses the current locale’s decimal point for recognizingnumbers.strtonum is a gawk extension; it is not available in compatibility mode (seeSection 11.2 [Command-Line Options], page 177).sub(regexp, replacement [, target])The sub function alters the value of target. It searches this value, which istreated as a string, for the leftmost, longest substring matched by the regularexpression regexp. Then the entire string is changed by replacing the matchedtext with replacement. The modified string becomes the new value of target.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.This function is peculiar because target is not simply used to compute a value,and not just any expression will do—it must be a variable, field, or array elementso that sub can store a modified value there. If this argument is omitted, thenthe default is to use and alter $0. 4 For example:str = "water, water, everywhere"sub(/at/, "ith", str)sets str to "wither, water, everywhere", by replacing the leftmost longestoccurrence of ‘at’ with ‘ith’.The sub function returns the number of substitutions made (either one or zero).If the special character ‘&’ appears in replacement, it stands for the precisesubstring that was matched by regexp. (If the regexp can match more than onestring, then this precise substring may vary.) For example:{ sub(/candidate/, "& and his wife"); print }changes the first occurrence of ‘candidate’ to ‘candidate and his wife’ oneach input line. Here is another example:$ awk ’BEGIN {> str = "daabaaa"> sub(/a+/, "C&C", str)> print str> }’⊣ dCaaCbaaaThis shows how ‘&’ can represent a nonconstant string and also illustrates the“leftmost, longest” rule in regexp matching (see Section 2.7 [How Much TextMatches?], page 33).The effect of this special character (‘&’) can be turned off by putting a backslashbefore it in the string. As usual, to insert one backslash in the string, you must4 Note that this means that the record will first be regenerated using the value of OFS if any fields havebeen changed, and that the fields will be updated after the substitution, even if the operation is a “no-op”such as ‘sub(/^/, "")’.

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

Saved successfully!

Ooh no, something went wrong!