The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed to find a pattern shyjuezy UNIX for Dummies Questions & Answers 2 05-22-2008 10:03 AM
Count of matched pattern occurance palash2k UNIX for Dummies Questions & Answers 3 04-24-2008 04:33 PM
String search - Command to find second occurance saurabhsinha23 UNIX for Dummies Questions & Answers 5 12-06-2007 08:03 PM
Pattern searching pattern in c files murthybptl Shell Programming and Scripting 6 11-17-2007 09:15 AM
find pattern in FILES and replace it ?? tamer UNIX for Dummies Questions & Answers 4 03-03-2001 10:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-30-2008
Prahlad Prahlad is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 11
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.

Thanks,
  #2 (permalink)  
Old 04-30-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Quote:
Originally Posted by Prahlad View Post
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.

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 (permalink)  
Old 04-30-2008
Shivdatta Shivdatta is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 73

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

  #4 (permalink)  
Old 04-30-2008
Prahlad Prahlad is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 11
Thank you all for reply.

I will try both solutions.

Thanks,
  #5 (permalink)  
Old 04-30-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 04-30-2008
chappidi_pradee chappidi_pradee is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 11
hello try this one, hope this helped you

find ./ dir_name -exec grep "SHELL" '{}' \; -print;
  #7 (permalink)  
Old 04-30-2008
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
Quote:
Originally Posted by Prahlad View Post
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.
Closed Thread

Bookmarks

Tags
grep or

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0