tags:

views:

51

answers:

3

Hi

I have the following file

your  Answer  = AA
your  Answer  = AB
your  Answer  = CD
your  Answer  = XY 
your  Answer  = DD

to verify all answers I run the following awk

echo "your  Answer  = AA" |  awk '/= AA/{print " passed "}'
echo "your  Answer  = AA" |  awk '/= AB/{print " passed "}'
echo "your  Answer  = AA" |  awk '/= CD/{print " passed "}'
echo "your  Answer  = AA" |  awk '/= XY/{print " passed "}'
echo "your  Answer  = AA" |  awk '/= DD/{print " passed "}'

My question: How to do the same on one awk command in pleace to run 5 awk command?

Like

echo "your Answer = AA" | awk '/= AA|AB|CD|XY|DD/{print " passed "}'

A: 
awk -F"=" '$2~/A[AB]|[CD]D|XY/{print "passed"}' file
ghostdog74
not goodbecause echo " blabla = blabla AA " | awk -F"=" '$2~/A[AB]|[CD]D|XY/{print "passed"}' also print passed , in this case its must be not print passed because after "=" we have blabla and not AA please advice?
lidia
that's because AA now is not a field 2 ($2). If your grades are all over the place, use $0.
ghostdog74
not good " echo " param1 param2 param3 = param1 AA " | awk -F"=" '$0~/AA|AB/{print "passed"}' , its also print passed??????????
lidia
A: 
or(echo "your  Answer  = AA",echo "your  Answer  = AA") | awk '/= AA/{print " passed "}'

Try something like this.

Kex
where is the OR rule ?and if I have spaces between "=" to AA or AB ??
lidia
The sintax was or(value1,value2), the spaces don't matter.
Kex
+1  A: 

You're close: you just need to put the alternates in a group so the equal sign is always there:

echo "your Answer = AA" | awk '/= (AA|AB|CD|XY|DD)/ {print " passed "}'
glenn jackman