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 13: Practical awk Programs 255The shell construct ‘‘...‘’ is called command substitution. The output of the commandbetween the two backquotes (grave accents) is substituted into the command line. It is savedas a single string, even if the results contain whitespace.The expanded program is saved in the variable processed_program. It’s done in thesesteps:1. Run gawk with the ‘@include’-processing program (the value of the expand_prog shellvariable) on standard input.2. Standard input is the contents of the user’s program, from the shell variable program.Its contents are fed to gawk via a here document.3. The results of this processing are saved in the shell variable processed_program byusing command substitution.The last step is to call gawk with the expanded program, along with the original optionsand command-line arguments that the user supplied.eval gawk $opts -- ’"$processed_program"’ ’"$@"’The eval command is a shell construct that reruns the shell’s parsing process. Thiskeeps things properly quoted.This version of igawk represents my fourth attempt at this program. There are four keysimplifications that make the program work better:• Using ‘@include’ even for the files named with ‘-f’ makes building the initial collectedawk program much simpler; all the ‘@include’ processing can be done once.• Not trying to save the line read with getline in the pathto function when testing forthe file’s accessibility for use with the main program simplifies things considerably.• Using a getline loop in the BEGIN rule does it all in one place. It is not necessary tocall out to a separate loop for processing nested ‘@include’ statements.• Instead of saving the expanded program in a temporary file, putting it in a shell variableavoids some potential security problems. This has the disadvantage that the script reliesupon more features of the sh language, making it harder to follow for those who aren’tfamiliar with sh.Also, this program illustrates that it is often worthwhile to combine sh and awk programmingtogether. You can usually accomplish quite a lot, without having to resort tolow-level programming in C or C++, and it is frequently easier to do certain kinds of stringand argument manipulation using the shell than it is in awk.Finally, igawk shows that it is not always necessary to add new features to a program;they can often be layered on top. With igawk, there is no real reason to build ‘@include’processing into gawk itself.As an additional example of this, consider the idea of having two files in a directory inthe search path:‘default.awk’This file contains a set of default library functions, such as getopt and assert.‘site.awk’This file contains library functions that are specific to a site or installation;i.e., locally developed functions. Having a separate file allows ‘default.awk’ to

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

Saved successfully!

Ooh no, something went wrong!