in a unix shell script I have
filename=myfile
LOG=/dir/log*
j=`awk '/AAAA/ && /BBB/ && /${filename}/ && /CCC/' ${LOG}`
but when the script runs, with set -x I see:
awk /AAA/ && /BBB/ && /${filename}/ && /CCC/ /dir/log1 /dir/log2
How can I escape the ${filename} variable within the awk argument so that it resolves correctly ?
To illustrate what I am trying to do
I have a file called /tmp/S20150814.001 which contains
This line contains AAAA and BBB and CCC and bcd_input_13082015_0800.txt
In my script, if I use
MYLOG=/tmp/S20150814.001
j=`awk '/AAAA/ && /BBB/ && /bcd_input_13082015_0800.txt/ && /CCC/' ${MYLOG}`
if [[ ${#j} -gt 0 ]]
then
and run the script I see
+ + awk /AAAA/ && /BBB/ && /bcd_input_13082015_0800.txt/ && /CCC/ /tmp/S20150814.001
j=This line contains AAAA and BBB and CCC and bcd_input_13082015_0800.txt
+ [[ 71 -gt 0 ]]
but if I change the script to
MYLOG=/tmp/S20150814.001
filename=bcd_input_13082015_0800.txt
j=$(awk -v filename="$filename" '/AAAA/ && /BBB/ && $0==filename && /CCC/' ${MYLOG})
if [[ ${#j} -gt 0 ]]
then
and run it, I get
+ + awk -v filename=bcd_input_13082015_0800.txt /AAAA/ && /BBB/ && $0==filename && /CCC/ /tmp/S20150814.001
j=
+ [[ 0 -gt 0 ]]