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 141You type sub sees sub generates\& & the matched text\\& \& a literal ‘&’\\\& \& a literal ‘&’\\\\& \\& a literal ‘\&’\\\\\& \\& a literal ‘\&’\\\\\\& \\\& a literal ‘\\&’\\q \q a literal ‘\q’Table 8.1: Historical Escape Sequence Processing for sub and gsubThis table shows both the lexical-level processing, where an odd number of backslashesbecomes an even number at the runtime level, as well as the runtime processing done bysub. (For the sake of simplicity, the rest of the following tables only show the case of evennumbers of backslashes entered at the lexical level.)The problem with the historical approach is that there is no way to get a literal ‘\’followed by the matched text.The 1992 POSIX standard attempted to fix this problem. That standard says that suband gsub look for either a ‘\’ or an ‘&’ after the ‘\’. If either one follows a ‘\’, that characteris output literally. The interpretation of ‘\’ and ‘&’ then becomes as shown in Table 8.2.You type sub sees sub generates& & the matched text\\& \& a literal ‘&’\\\\& \\& a literal ‘\’, then the matched text\\\\\\& \\\& a literal ‘\&’Table 8.2: 1992 POSIX Rules for sub and gsub Escape Sequence ProcessingThis appears to solve the problem. Unfortunately, the phrasing of the standard is unusual.It says, in effect, that ‘\’ turns off the special meaning of any following character, but foranything other than ‘\’ and ‘&’, such special meaning is undefined. This wording leads totwo problems:• Backslashes must now be doubled in the replacement string, breaking historical awkprograms.• To make sure that an awk program is portable, every character in the replacementstring must be preceded with a backslash. 6Because of the problems just listed, in 1996, the gawk maintainer submitted proposedtext for a revised standard that reverts to rules that correspond more closely to the original6 This consequence was certainly unintended.

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

Saved successfully!

Ooh no, something went wrong!