Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
awk '$1 == $2 {n++}; END {print n}'
share|improve this answer

Your Answer


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.