21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8: Functions 143parenthesized subexpression is placed in the generated output. Otherwise, no matter whatcharacter follows the ‘\’, it appears in the generated text and the ‘\’ does not, as shown inTable 8.5.You type gensub sees gensub generates& & the matched text\\& \& a literal ‘&’\\\\ \\ a literal ‘\’\\\\& \\& a literal ‘\’, then the matched text\\\\\\& \\\& a literal ‘\&’\\q \q a literal ‘q’Table 8.5: Escape Sequence Processing for gensubBecause of the complexity of the lexical and runtime level processing and the specialcases for sub and gsub, we recommend the use of gawk and gensub when you have to dosubstitutions.Advanced Notes: Matching the Null StringIn awk, the ‘*’ operator can match the null string. This is particularly important for thesub, gsub, and gensub functions. For example:$ echo abc | awk ’{ gsub(/m*/, "X"); print }’⊣ XaXbXcXAlthough this makes a certain amount of sense, it can be surprising.8.1.4 Input/Output FunctionsThe following functions relate to input/output (I/O). Optional parameters are enclosed insquare brackets ([ ]):close(filename [, how])Close the file filename for input or output. Alternatively, the argument may bea shell command that was used for creating a coprocess, or for redirecting toor from a pipe; then the coprocess or pipe is closed. See Section 4.8 [ClosingInput and Output Redirections], page 71, for more information.When closing a coprocess, it is occasionally useful to first close one end of thetwo-way pipe and then to close the other. This is done by providing a secondargument to close. This second argument should be one of the two stringvalues "to" or "from", indicating which end of the pipe to close. Case inthe string does not matter. See Section 10.2 [Two-Way Communications withAnother Process], page 170, which discusses this feature in more detail andgives an example.fflush([filename])Flush any buffered output associated with filename, which is either a file openedfor writing or a shell command for redirecting output to a pipe or coprocess.

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

Saved successfully!

Ooh no, something went wrong!