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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find files which has more than one occurance of pattern
# 1  
Old 04-30-2008
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. Smilie

Thanks,
# 2  
Old 04-30-2008
Quote:
Originally Posted by Prahlad
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. Smilie

Thanks,
Code:
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" *
# 3  
Old 04-30-2008
Code:
for file in `ls -1`
do
count=`grep -c "SHELL" $file`
if [ $count -gt 1 ]
then
echo $file 
fi
done

# 4  
Old 04-30-2008
Thank you all for reply. Smilie

I will try both solutions.

Thanks,
# 5  
Old 04-30-2008
The first one just greps, the second will list files which don't have any matches at all.

But try this:

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

... provided your grep can do -r (recurse a directory).
# 6  
Old 04-30-2008
hello try this one, hope this helped you

find ./ dir_name -exec grep "SHELL" '{}' \; -print;
# 7  
Old 04-30-2008
Quote:
Originally Posted by Prahlad
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.
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

2. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

3. Shell Programming and Scripting

sed pattern to delete lines containing a pattern, except the first occurance

Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows. header for file 1111 2222 3333 header for file 1111 2222 3333 header for file... (8 Replies)
Discussion started by: gary_w
8 Replies

4. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

5. Shell Programming and Scripting

Need 10 lines before the first occurance of a pattern

Here is the text file: 1This is a text file 2this is a text file 3This is a text file 4this is a text file 5This is a text file 6this is a text file 7This is a text file 8this is a text file 9This is a text file 10this is a text file 11This is a text file 12this is a text file 13This... (4 Replies)
Discussion started by: Johny001
4 Replies

6. Shell Programming and Scripting

Pattern matching first occurance only wanted

I have the following code: declare -a search search=(name label elapsed totalfilecount totalfilebytes starttime exitcode errors warnings fatals) for (( i = 0 ; i < ${#search} ; i++ )) do data=`awk -F '^'${search}'=\"' 'BEGIN{RS="\" "; OFS="\n"; ORS=""} { print $2 }' summary.alg` ... (1 Reply)
Discussion started by: Ikon
1 Replies

7. UNIX for Dummies Questions & Answers

Replace first 5 occurance of a pattern

There is scenario that i have to replace a pattern at the first 5 occurance in a file. say i need to replace 'abc' by 'xyz' at its first 5 occurance in a file a.txt, can anybody help me how it can be done, i can do complete replacement of the pattern using sed throughtout the file. (1 Reply)
Discussion started by: palash2k
1 Replies

8. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

9. Shell Programming and Scripting

How to insert values in 1st occurance out of two occurance in a file

Hi I have a file which contains the following two lines which are same But I would like to insert the value=8.8.8.8 in the 1st occurance line and value=9.9.9.9 in the 2nd occurance line. <parameter name="TestIp1" value=""> <parameter name="TestIp1" value=""> Please suggest (1 Reply)
Discussion started by: madhusmita
1 Replies

10. UNIX for Dummies Questions & Answers

Count of matched pattern occurance

In a file a pattern is occured many times randomly. Even it may appear more then once in the same line too. How i can get the number of times that pattern appeared in the file? let the file name is abc.txt and the pattern is "xyz". I used the following code: grep -ic "xyz" abc.txt but it is... (3 Replies)
Discussion started by: palash2k
3 Replies
Login or Register to Ask a Question