Take the 2-minute tour ×

Is it possible to write an awk one-liner which will compare two fields on the same line to see if they are equal, and output the number of lines where this is true?

I tried:

awk '$1==$2 { print($1 $2); }' foo1 | wc -l     

I don't think this is correct.

share|improve this question
it seems to work for me. why do you say not correct? –  gokcehan Oct 26 '12 at 20:58
Very sorry, I had != instead of == running. –  turtle Oct 26 '12 at 21:03
btw you can just print only one of the fields (e.g print $1) since you're just counting the lines.. –  gokcehan Oct 26 '12 at 21:04
Or print the whole line by just using print –  Bernhard Nov 2 '12 at 8:48
add comment

1 Answer

awk '$1 == $2 {n++}; END {print n}'
share|improve this answer
add comment

Your Answer


