Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

1 Answer 1

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