Arun
Arun

Reputation: 2572

grep the count of occurences of a pattern for report generation

I want to find a specific pattern of occurences in multiple files and need to generate a report. For example, if there are two files say test1.txt and text2.txt. I will have to go through the files and look the count for "samplePattern" in each and every file. And create a report like

<filename>  <count>
text1.txt   3
text2.txt   10

I have used the following command in the directory where I have all the files:

grep -lr -o 'samplePattern'  | wc -l

But this gives me a cumulative count of all the 'samplePattern' in all the files.

Upvotes: 0

Views: 171

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272257

Why not use grep -c 'samplePattern' *, which will count per file ?

Upvotes: 5

Related Questions