tags:

views:

48

answers:

2

hi all

I have a problem with the following awk syntax

echo " param1 param2 param3 = param1 AA , AB , AC , AD  "  | awk -F"=" '$2~/AA|AB|AC|AD/{print "passed"}'

The awk prints "passed", but it shouldn't be because after "=" I have "param1" and not "AA" or AB", etc.

The target of the awk is to print "passed" only if the string after "=" is AA OR AB OR AC OR AD.

and if I have something else after "=" then its not should print passed

how to fix the awk syntax?

lidia

+1  A: 

You need anchors:

awk -F= '$2 ~ /^(AA|AB|AC|AD)$/ {print "passed"}'

If you want to allow spaces:

awk -F= '$2 ~ /^ *(AA|AB|AC|AD) *$/ {print "passed"}'
unbeli
echo " param1 param2 param3 = AA " | awk -F= '$2 ~ /^(AA|AB|AC|AD)$/ {print "passed"}' , its not print passed why???
lidia
because you have spaces around AA. " AA" is not the same as "AA".
unbeli
need to support spaces also
lidia
that's simple, see updated answer
unbeli
echo " param1 param2 param3 = AA " | awk -F= '$2 ~ /^ *(AA|AB|AC|AD)$/ {print "passed"}' , its not work if I have space after AA , please what to do??
lidia
come on, don't be that helpless, do a little bit of work yourself!
unbeli
A: 

This should work:

echo " param1 param2 param3 = param1 AA , AB , AC , AD  "  | 
awk -F"=" -v var="passed" '$2~/AA|AB|AC|AD/{printf "%s",var}'
Vijay Sarathi
its linux machine not windows
lidia
its not work " ^ backslash not last character on line"
lidia