Suresh Kumar
Suresh Kumar

Reputation: 675

How to get the no of matched occurrences using grep command in linux?

If we use grep -c option it will give you the each occurrences only once per line. But I need the total no of matched occurrences not line count.

Upvotes: 1

Views: 51

Answers (2)

Anurag Peshne
Anurag Peshne

Reputation: 1547

You can use -o flag to output only the matched part and then pipe it to wc -w to get word count.

Eg: ls ~ | grep -o pattern | wc -w

Upvotes: 1

svikkiram
svikkiram

Reputation: 86

Use this grep -o pattern path | wc -l

Upvotes: 1

Related Questions