I have a shell script that constructs an awk program as a string then pass that string to awk. This is because I want to use values of shell variables in the awk program.
My code looks like this:
awk_prog="'{if (\$4~/$shell_var/) print \$1,\$2}'"
echo $awk_prog
awk $awk_prog $FILENAME
However, when I pass the string to awk, I always get the error:
'{if ($4~/regex/) print $1,$2}'
awk: '{if
awk: ^ invalid char ''' in expression
What does that error message mean? I tried the -F: switch but it does not help. How can I settle this issue?
Thank you.
This is caused by shell quoting. The following will work:
When you run
awk '{ print }' foofrom the command line, the shell interprets and removes the quotes around the program so awk receives two arguments – the first is the program text and the second is the filenamefoo. Your example was sending awk the program text'{if ...}'which is invalid syntax as far as awk is concerned. The outer quotes should not be present.In the snippet that I gave above, the shell uses the quotes in the
awk_prog=line to group the contents of the string into a single value and then assigns it to the variableawk_prog. When it executes theawk "$awk_prog"...line, you have to quote the expansion of$awk_progso awk receives the program text as a single argument.