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 6: Patterns, Actions, and Variables 101The most common method is to use shell quoting to substitute the variable’s value intothe program inside the script. For example, in the following program:echo -n "Enter search pattern: "read patternawk "/$pattern/ "’{ nmatches++ }END { print nmatches, "found" }’ /path/to/datathe awk program consists of two pieces of quoted text that are concatenated together toform the program. The first part is double-quoted, which allows substitution of the patternvariable inside the quotes. The second part is single-quoted.Variable substitution via quoting works, but can be potentially messy. It requires a goodunderstanding of the shell’s quoting rules (see Section 1.1.6 [Shell-Quoting Issues], page 14),and it’s often difficult to correctly match up the quotes when reading the program.A better method is to use awk’s variable assignment feature (see Section 5.3.2 [AssigningVariables on the Command Line], page 78) to assign the shell variable’s value to an awkvariable’s value. Then use dynamic regexps to match the pattern (see Section 2.8 [UsingDynamic Regexps], page 34). The following shows how to redo the previous example usingthis technique:echo -n "Enter search pattern: "read patternawk -v pat="$pattern" ’$0 ~ pat { nmatches++ }END { print nmatches, "found" }’ /path/to/dataNow, the awk program is just one single-quoted string. The assignment ‘-vpat="$pattern"’ still requires double quotes, in case there is whitespace in the valueof $pattern. The awk variable pat could be named pattern too, but that would bemore confusing. Using a variable also provides more flexibility, since the variable can beused anywhere inside the program—for printing, as an array subscript, or for any otheruse—without requiring the quoting tricks at every point in the program.6.3 ActionsAn awk program or script consists of a series of rules and function definitions interspersed.(Functions are described later. See Section 8.2 [User-Defined Functions], page 153.) A rulecontains a pattern and an action, either of which (but not both) may be omitted. Thepurpose of the action is to tell awk what to do once a match for the pattern is found. Thus,in outline, an awk program generally looks like this:[pattern] [{ action }][pattern] [{ action }]...function name(args) { ... }...An action consists of one or more awk statements, enclosed in curly braces (‘{...}’).Each statement specifies one thing to do. The statements are separated by newlines orsemicolons. The curly braces around an action must be used even if the action containsonly one statement, or if it contains no statements at all. However, if you omit the actionentirely, omit the curly braces as well. An omitted action is equivalent to ‘{ print $0 }’:

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

Saved successfully!

Ooh no, something went wrong!