Using Awk to search for spaces with spaces

I am having trouble finding the last occurrence of a string in a file using awk. I pass the string to the example script "Ping Error for hostname". I keep getting awk: ^ unterminated string. #! / Bin / w

LOG=/opt/netcool/omnibus/log/mttrapd.log TMP_FILE=sitescope.$$ args="$*" #ruby sitescope.rb echo "looking for $1 " tail -1000 $LOG > $TMP_FILE echo "WORD = $args" awk '"/'$args'/" {f=$0} END{print f}' $TMP_FILE > data.out rm -f $TMP_FILE 
+2
awk
Feb 23 2018-12-18T00:
source share
2 answers

The point of single quotes around the awk line is to store everything in the first argument (and prevent shell replacement). You can be more flexible with how you combine this argument as

 awk "/$args/"' {f=$0} END{print f}' $TMP_FILE > data.out 
+2
Feb 23 '12 at 18:07
source share
β€” -

Instead of playing around quotes, pass an awk shell variable with the -v option

 awk -v pattern="$*" 'match($0, pattern) {f=$0} END {print f}' 
+4
Feb 23 '12 at 18:56
source share



All Articles