2

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.

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

1 Answer 1

6
awk '$1 == $2 {n++}; END {print n}'

You must log in to answer this question.

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