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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Looping question hemangjani Shell Programming and Scripting 2 02-24-2006 09:47 AM
search question kf199 UNIX Desktop for Dummies Questions & Answers 1 12-27-2005 03:20 PM
Perl question - looping through an array of hashrefs kregh99 Shell Programming and Scripting 2 03-19-2004 07:48 AM
nested looping question Sn33R Shell Programming and Scripting 7 09-26-2003 11:14 AM
looping question hedrict UNIX for Dummies Questions & Answers 1 03-11-2002 01:19 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 11-15-2006
Registered User
 

Join Date: Jun 2006
Posts: 39
looping search question

This is my requirement--

I have a list file that contains a list of 50 words.
eg
word1
word2
word3

I want to search/grep for each of those words in a directory/and subdirectories and list the file in which that word exists.


Ne ideas for script/command?

I know grep -r <pattern> /direaddress will search for existence of a pattern in all files in a directory...but how will i grep for a pattern in a filelist in directory/subdirectories.

Last edited by alfredo123; 11-15-2006 at 01:03 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 11-15-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,261
Code:
find /path -type f | \
while read file do
    grep -l  -f /path/to/wordfile  "$file"
done > list_of_found_files
Reply With Quote
  #3  
Old 11-15-2006
Registered User
 

Join Date: Jun 2006
Posts: 39
Thnx

Thanks a heap Jim...I think the below did the job for me and I needed to search the highest directory to see results from subdirectories.

Code:
!#usr/bin/ksh
#Example usage greptest.sh <filelist> <directory to search>

while read line
    do
    grep -r ${line} $2 
    done <$1
    
exit 0
Reply With Quote
  #4  
Old 11-16-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
Quote:
Originally Posted by jim mcnamara
Code:
find /path -type f | \
while read file do
    grep -l  -f /path/to/wordfile  "$file"
done > list_of_found_files
The while loop can be replaced by the xargs command :
Code:
find /path -type f | xargs grep -l -f /path/to/wordfile  > list_of_found_files
The -r option doesn't exist for grep on all Unix flavors, it why we use a find command.

Jean-Pierre.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0