How to pass BASH shell variables into AWK statement
You have to use -v
, Getting shell variables into awk
may be done in several ways. Some are better than others.
This is the best way to do it (Please note use a space after -v
or it will be less portable. E.g., awk -v var=
not awk -vvar
)
# your shell variables
marker1="###___showhost___###"
marker2="###___showhostset___###"
# and passing them to awk
awk -v market1="$market1" -v market2="$market2" '
$0 ~ "^"market1{found=1;next}
$0 ~ "^"market2{found=0}found' file.in
Example:
akshay@db-3325:~$ foo="this is my shell variable"
akshay@db-3325:~$ awk -v myvar="$foo" 'BEGIN{print myvar}'
this is my shell variable