Issue
How can I count all matches with grep, when there are more than one match per line?
$ cat example.txt
int foo(int a, char b) { int i = 80; return i; }
$ grep -c "int" example.txt
1
I want to output 3
(since int
appears 3 times in the file)
Solution
With grep
that supports -o
option:
$ grep -o 'int' ip.txt | wc -l
3
With ripgrep:
$ rg -oc 'int' ip.txt
3
Answered By - Sundeep