bash variable inside awk

From thelinuxwiki
Jump to: navigation, search

taken from How to use shell variables in an awk script

This is the best way to do it. It uses the -v option: (P.S. use a space after -v or it will be less portable. E.g., awk -v var= not awk -vvar)

variable="line one\nline two"
awk -v var="$variable" 'BEGIN {print var}'
line one
line two