1

I have 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 print passed , but its not shuld 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

2 Answers 2

2

After the =, you have the string param1 AA , AB , AC , AD. Your awk expression accepts any string that contains AA or AB or AC or AD. If you want these words to appear at the beginning of the field, you need to anchor the pattern (start it with ^ to match only at the beginning, finish it with '$' to match only at the end). For example the following expression requires the field to be only one of the specified words, possibly with spaces before and after:

$2 ~ /^ *(AA|AB|AC|AD) *$/
1
  • Better, more complete answer than mine.
    – W_Whalley
    Jul 24, 2010 at 13:38
1

This awk expression to search for AA at the beginning of the field is ^AA, or to allow zero or more spaces between = and the AA, it is ^ *AA, so this expression ought to only print passed if the first term after the = is AA OR BB OR AC OR AD preceded by zero or more spaces.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .