10.2 Variables in awk
You can define variables within the awk command. They can be dynamically defined and contain both numbers and strings. Note that the defined variable is only valid within that specific awk command and will be forgotten after the last single quote. Multiple actions can be performed in one command and are separated by a semicolon (;):
Variables can be increased by 1 for each entry via the ++ operator. This can easily be used to count lines by starting with x=0 before the first line (using BEGIN), and increasing x with every line. In the following example, we count the lines that match the pattern “ID”:
As mentioned above, the variables within awk are only valid within one command. Likewise, bash variables from outside of awk must first be introduced.
Attention: Using a previously undefined variable will not necessarily throw an error, but it will be simply have a zero (numeric) or empty (string) value:
$ #trying to access an awk variable outside of awk
$ echo "5 7" | awk '{x=10; print $1*x; x=x+$2; print x}'
$ echo "this variable does not exist: $x"
50
17
this variable does not exist: $ #trying to access a bash variable inside of awk
$ a=5
$ echo "5 7" | awk '{print $1+a, 10-a ; print a}'
5 10Instead, bash variables should be passed to awk with the -v command before the first single quote: