Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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) *$/
share|improve this answer
    
Better, more complete answer than mine. –  W_Whalley Jul 24 '10 at 13:38

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"}'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.