[awk] using shell variable

Posted on July 15, 2013, 1:07 PM

Shell variable can be ported into awk command by using "-v" flag:

#!/bin/bash
var=100
awk -v var=$var '{print $1*var}' file.dat
An important note here is that within the awk command, calling the var does not require the '$' sign.

If there are more than one shell variable to call, use -v again and put space in between:
awk -v var=$var -v var2=$var2 '{print $1*var, var2}' file.dat
This will print $1*$var and $var2.