grep - match files containing minimum number of pattern matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep - match files containing minimum number of pattern matches
# 1  
Old 01-12-2012
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:
PHP Code:
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
# 2  
Old 01-12-2012
I would approach it with something like this:

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

Prints the filenames which contain more than two " said:" strings.
This User Gave Thanks to agama For This Post:
# 3  
Old 01-13-2012
Code:
 
grep -m 3 -l " said:" *

# 4  
Old 01-13-2012
If you have GNU awk this should work:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

2. Shell Programming and Scripting

Number of matches and matched pattern(s) in awk

input: !@#$%2QW5QWERTAB$%^&* The string above is not separated (or FS=""). For clarity sake one could re-write the string by including a "|" as FS as follow: !|@|#|$|%|2QW|5QWERT|A|B|$|%|^|&|* Here, I am only interested in patterns (their numbers are variable between records) containing... (16 Replies)
Discussion started by: beca123456
16 Replies

3. Shell Programming and Scripting

Count number of pattern matches per line for all files in directory

I have a directory of files, each with a variable (though small) number of lines. I would like to go through each line in each file, and print the: -file name -line number -number of matches to the pattern /comp/ for each line. Two example files: cat... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

Pattern match using grep between two files

Hello Everyone , I have two files. I want to pick line from file-1 and match with the complete data in file-2 , if there is a match print all the match lines in file 3. Below is the file cat test1.txt vikas vikasjain j ain testt douknow hello@vik@ # 33 ||@@ vcpzxcmvhvdsh... (1 Reply)
Discussion started by: mailvkjain
1 Replies

5. Shell Programming and Scripting

Finding log files that match number pattern

I have logs files which are generated each day depending on how many processes are running. Some days it could spin up 30 processes. Other days it could spin up 50. The log files all have the same pattern with the number being the different factor. e.g. LOG_FILE_1.log LOG_FILE_2.log etc etc ... (2 Replies)
Discussion started by: atelford
2 Replies

6. UNIX for Dummies Questions & Answers

extracting lates pattern match from multiple matches in log

Hi, I have a large, multiline log file. I have used pcregrep to extract all entries in that log that match a particular pattern - where that pattern spans multiple lines. However, because the log file is large, and these entries occur every few minutes, I still output a very large amount... (6 Replies)
Discussion started by: dbrb2
6 Replies

7. Shell Programming and Scripting

Pattern to match decimal number and interger

Hi, i've a code if (($A && ((!($A =~ /^\d+$/))))) { -- -- not a number } else { --- its number. } which matches for integer value, i need to modify that pattern where it matches decimal number with 2 decimal points and also integer value. for eg: values 10, 10.00. 0.1, 1 , 3 must... (2 Replies)
Discussion started by: asak
2 Replies

8. Shell Programming and Scripting

Using grep returns partial matches, I need to get an exact match or nothing

I’m trying to modify someone perl script to fix a bug. The piece of code checks that the zone name you want to add is unique. However, when the code runs, it finds a partial match using grep, and decides it already exists, so the “create” command exits. $cstatus = `${ZADM} list -vic | grep... (3 Replies)
Discussion started by: TKD
3 Replies

9. Shell Programming and Scripting

Extracting N lines match number X of a pattern

Hi All, as the title says I need to extract N lines after match number X of a pattern. e.g. 111 xxx xxx 111 yyy yyy 111 www www 111 zzz zzz I would like to extract the two lines after the second 111 occurrence. I tried with grep but I didn't find any options to do that. Any... (11 Replies)
Discussion started by: f_o_555
11 Replies

10. UNIX for Dummies Questions & Answers

how to use pattern match with grep

hi, i'm having problem like this. i wish to grep some keyword from certain files in one directory. let's say i have a lot of files in my directory with the name of file.1 file.2 file.3 ...... file.500 i only wish to grep the keyword from file.20, file.21, file.23 .... file.50 i tried... (5 Replies)
Discussion started by: rei
5 Replies
Login or Register to Ask a Question