awk problem with quotes

can someone help me with this. i keep getting errors:

var1="MaxClients"
var2="java|could not.*problem found|panic() failure seen|aborting [ERROR]"
awk 'NR>=1&&NR<=10 && /'${var1}'/ && !/'${var2}'/ {++c}c==3{o=$0 RS $0 RS $0; print o; c=0}' log

when i run the above, i keep getting:

awk: NR>=1&&NR<=10 && /MaxClients/ && !/java|could
awk:                                    ^ unterminated regexp

if anybody is able to fix the above, i'm also interested in how i can modify it so it gives the count of lines returned, instead of showing the lines. something like:

awk 'NR>=1&&NR<=10 && /'${var1}'/ && !/'${var2}'/ {++c}c==3{o=$0 RS $0 RS $0; print o; c=0}' log
4
awk -v var1="$var1" -v var2="$var2" 'NR>=1&&NR<=10 && $0 ~ var1 && $0 !~ var2 {...}'

PS: I am not sure what the test "NR>=1" is expected to do.

NR>=1 is not need since you always start at record #1

More a shell quoting question:

awk 'NR>=1&&NR<=10 && /'"${var1}"'/ && !/'"${var2}"'/ {++c}c==3{o=$0 RS $0 RS $0; print o; c=0}' log

When you leave the shelter of the ', at least wear a " so your spaces do not confuse the shell!

1 Like