manpagez: man pages & more
info gawk
Home | html | info | man

gawk: Using Shell Variables

 
 7.2 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.
 
    A common method is to use shell quoting to substitute the variable's
 value into the program inside the script.  For example, consider the
 following program:
 
      printf "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' shell variable
 inside the quotes.  The second part is single-quoted.
 
    Variable substitution via quoting works, but can potentially be
 messy.  It requires a good understanding of the shell's quoting rules
 (⇒Quoting), and it's often difficult to correctly match up the
 quotes when reading the program.
 
 Assignment Options::) to assign the shell variable's value to an 'awk'
 variable.  Then use dynamic regexps to match the pattern (⇒Computed
 Regexps).  The following shows how to redo the previous example using
 this technique:
 
      printf "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, as 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.
 
© manpagez.com 2000-2018
Individual documents may contain additional copyright information.