Using Shell Variables in Programs
=================================
`awk' programs are often used as components in larger programs
written in shell. For example, it is very common to use a shell
variable to hold a pattern that the `awk' program searches for. There
are two ways to get the value of the shell variable into the body of
the `awk' program.
The most common method is to use shell quoting to substitute the
variable's value into the program inside the script. For example, in
the following program:
echo -n "Enter search pattern: "
read pattern
awk "/$pattern/ "'{ nmatches++ }
END { print nmatches, "found" }' /path/to/data
the `awk' program consists of two pieces of quoted text that are
concatenated together to form the program. The first part is
double-quoted, which allows substitution of the `pattern' variable
inside the quotes. The second part is single-quoted.
Variable substitution via quoting works, but can be potentially
messy. It requires a good understanding of the shell's quoting rules
(Note:Shell Quoting Issues.), 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 (Note:Assigning Variables on the Command Line.) to
assign the shell variable's value to an `awk' variable's value. Then
use dynamic regexps to match the pattern (*note Using Dynamic Regexps:
Computed Regexps.). The following shows how to redo the previous
example using this technique:
echo -n "Enter search pattern: "
read pattern
awk -v pat="$pattern" '$0 ~ pat { nmatches++ }
END { print nmatches, "found" }' /path/to/data
Now, the `awk' program is just one single-quoted string. The
assignment `-v pat="$pattern"' still requires double quotes, in case
there is whitespace in the value of `$pattern'. The `awk' variable
`pat' could be named `pattern' too, but that would be more confusing.
Using a variable also provides more flexibility, since the variable can
be used anywhere inside the program--for printing, as an array
subscript, or for any other use--without requiring the quoting tricks
at every point in the program.