Abhai Kollara Abhai Kollara - 2 months ago 13
Linux Question

grep -c not working

Using grep (GNU grep) 2.16

I tried running

grep -Eo '<\/cite>' webpage
and get the following output

</cite>
</cite>
</cite>
</cite>
</cite>
</cite>
</cite>
</cite>
</cite>
</cite>
</cite>
</cite>
</cite>


Then I add the -c flag to count occurences
grep -cEo '<\/cite>' webpage
and get

3


Obviously the answer should be 13. Don't know what I'm doing wrong

Answer

grep -c counts lines matches, while -o shows the match only. i.e. with input:

abcabc
abcabc

Using grep -o 'a' on the above will output:

a
a
a
a

And grep -c 'a' will give

2

You can pipe to wc -l to count number of matches:

$ grep -o 'a' demo.txt | wc -l
4