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 13: Practical awk Programs 249}ARGV[1] = ARGV[2] = ""# look ma, no hands!{if (RT == "")printf "%s", $0elseprint}The program relies on gawk’s ability to have RS be a regexp, as well as on the setting ofRT to the actual text that terminates the record (see Section 3.1 [How Input Is Split intoRecords], page 36).The idea is to have RS be the pattern to look for. gawk automatically sets $0 to the textbetween matches of the pattern. This is text that we want to keep, unmodified. Then, bysetting ORS to the replacement text, a simple print statement outputs the text we want tokeep, followed by the replacement text.There is one wrinkle to this scheme, which is what to do if the last record doesn’t endwith text that matches RS. Using a print statement unconditionally prints the replacementtext, which is not correct. However, if the file did not end in text that matches RS, RT isset to the null string. In this case, we can print $0 using printf (see Section 4.5 [Usingprintf Statements for Fancier Printing], page 61).The BEGIN rule handles the setup, checking for the right number of arguments and callingusage if there is a problem. Then it sets RS and ORS from the command-line argumentsand sets ARGV[1] and ARGV[2] to the null string, so that they are not treated as file names(see Section 6.5.3 [Using ARGC and ARGV], page 116).The usage function prints an error message and exits. Finally, the single rule handlesthe printing scheme outlined above, using print or printf as appropriate, depending uponthe value of RT.13.3.9 An Easy Way to Use Library FunctionsUsing library functions in awk can be very beneficial. It encourages code reuse and thewriting of general functions. Programs are smaller and therefore clearer. However, usinglibrary functions is only easy when writing awk programs; it is painful when running them,requiring multiple ‘-f’ options. If gawk is unavailable, then so too is the <strong>AWK</strong>PATH environmentvariable and the ability to put awk functions into a library directory (see Section 11.2[Command-Line Options], page 177). It would be nice to be able to write programs in thefollowing manner:# library functions@include getopt.awk@include join.awk...# main programBEGIN {

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

Saved successfully!

Ooh no, something went wrong!