grep - match files containing minimum number of pattern matches

I want to search a bunch of files and list only those containing a minimum number of pattern matches. So if I want to identify files containing 3 (or more) instances of the pattern "said:" and I have file1 that contains the lines:
He said:
She said:

and file2 that contains the lines:
He said:
She said:
We said:

How do I match only file2?

The files that I want to search are in listed in a file called list.txt, I have tried:

cat list.txt | while read LST ; do egrep -m3 -iol  " said:"  $LST ; done

This obviously fails because I can only (apparently!) match a maximum number of pattern matches with grep.

TIA

I would approach it with something like this:

xargs <list.txt grep -c " said:" | awk -F :  '$2 > 2 { print $1 }'

Prints the filenames which contain more than two " said:" strings.

1 Like
 
grep -m 3 -l " said:" *

If you have GNU awk this should work:

 awk -vS="said:" -vN=3 'FNR==1 {c=0} $0~S&&++c>=N { print FILENAME;nextfile}' $(cat list.txt)