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.

142 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>existing practice. The proposed rules have special cases that make it possible to produce a‘\’ preceding the matched text. This is shown in Table 8.3.You type sub sees sub generates\\\\\\& \\\& a literal ‘\&’\\\\& \\& a literal ‘\’, followed by the matched text\\& \& a literal ‘&’\\q \q a literal ‘\q’\\\\ \\ \\Table 8.3: Proposed rules for sub and backslashIn a nutshell, at the runtime level, there are now three special sequences of characters(‘\\\&’, ‘\\&’ and ‘\&’) whereas historically there was only one. However, as in the historicalcase, any ‘\’ that is not part of one of these three sequences is not special and appears inthe output literally.gawk 3.0 and 3.1 follow these proposed POSIX rules for sub and gsub. The POSIXstandard took much longer to be revised than was expected in 1996. The 2001 standarddoes not follow the above rules. Instead, the rules there are somewhat simpler. The resultsare similar except for one case.The 2001 POSIX rules state that ‘\&’ in the replacement string produces a literal ‘&’,‘\\’ produces a literal ‘\’, and ‘\’ followed by anything else is not special; the ‘\’ is placedstraight into the output. These rules are presented in Table 8.4.You type sub sees sub generates\\\\\\& \\\& a literal ‘\&’\\\\& \\& a literal ‘\’, followed by the matched text\\& \& a literal ‘&’\\q \q a literal ‘\q’\\\\ \\ \Table 8.4: POSIX 2001 rules for subThe only case where the difference is noticeable is the last one: ‘\\\\’ is seen as ‘\\’ andproduces ‘\’ instead of ‘\\’.Starting with version 3.1.4, gawk follows the POSIX rules when ‘--posix’ is specified(see Section 11.2 [Command-Line Options], page 177). Otherwise, it continues to followthe 1996 proposed rules, since, as of this writing, that has been its behavior for over sevenyears.NOTE: At the next major release, gawk will switch to using the POSIX 2001rules by default.The rules for gensub are considerably simpler. At the runtime level, whenever gawk seesa ‘\’, if the following character is a digit, then the text that matched the corresponding

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

Saved successfully!

Ooh no, something went wrong!