How to find files which has more than one occurance of pattern

Hello Everyone,

Please help me in finding out the solution.

The problem is .. lets say i have 600 files in a directory. All 600 files are shell script files. Now i need to find out the files which contains a pattern "SHELL" more than once.

No matter how the pattern occurs , it can be in same line or in different lines also.

I hope there must be some fair way to do this. Please help me. :confused:

Thanks,

find /directoryname -name "*.sh" -o -name "any other pattern" | xargs grep -i "SHELL"

or just be in that directory and grep for the pattern

grep "pattern" *

for file in `ls -1`
do
count=`grep -c "SHELL" $file`
if [ $count -gt 1 ]
then
echo $file 
fi
done

Thank you all for reply. :slight_smile:

I will try both solutions.

Thanks,

The first one just greps, the second will list files which don't have any matches at all.

But try this:

grep -cr SHELL directory | grep -v ':[01]$'

... provided your grep can do -r (recurse a directory).

hello try this one, hope this helped you

find ./ dir_name -exec grep "SHELL" '{}' \; -print;

It seems to me that all the grep solutions that have been posted ignore the requirement of finding the pattern multiple times on the same line.

Ah yes, you are right, grep -c only counts the number of lines which match the pattern.

Assuming the pattern is supposed to be a word token, you could do something like

for f in directory/*; do
  tr '\011\040' '\012' <"$f" | grep -c SHELL | grep -v '^[01]$' >/dev/null && echo "$f"
done

The idea is to split each token onto a separate line, and then the number of matching lines will be equal to the number of occurrences of the token.

Different versions of tr have slightly different syntax; the first argument should be a tab and a space, and the second argument should be a newline. I'm using octal here because it's unambiguous, but not all versions of tr support that notation. Check the manual page of yours. If you need to pass in literal strings, it would look like

tr ' 	' '
'

where you probably have to type something like ctrl-v tab to write a literal tab. And yes, a newline in single quotes is a valid and useful string in shell syntax.

how to grep the pattern ?

File name: sample1.txt

ASD0000000000
RECS SD03342 Royal bengal
RECS SD03343 Chennai Kings
RECS SD03344 Mumbai Express
RECS SD03345 Rajasthan Royals
T0000000004

This is the binary file.When I am giving the command cat -v sampl1.txt I am getting the file in below format.

ASD0000000000^M
RECS SD03342 Royal bengal ^M
RECS SD03343 Chennai Kings ^M
RECS SD03344 Mumbai Express ^M
RECS SD03345 Rajasthan Royals ^M
T0000000004^M

How can I grep or find the pattern ^M from this files in the directory. I have couple of ascii files too.I have to grep the binary files.Please assist me

Thanks

Francis

Hi, Francis.

Welcome to the forum.

You might not get many replies because this thread has already been replied to for the original problem, and it doesn't appear that your question is directly related to multiple patterns.

I suggest you go to the the link Shell Programming and Scripting - The UNIX and Linux Forums and click the button New Thread to start a thread just for your question.

Best wishes ... cheers, drl

Another way using awk :

awk '{ count += gsub(/SHELL/, "") } END { print count }' inputfile

Jean-Pierre.