Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to recursively search for a list of keywords in a given directory?

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-14-2012   -   Original Discussion by neelmani
neelmani's Unix or Linux Image
neelmani neelmani is offline
Registered User
 
Join Date: May 2011
Last Activity: 8 January 2013, 12:07 PM EST
Posts: 48
Thanks: 28
Thanked 0 Times in 0 Posts
How to recursively search for a list of keywords in a given directory?

Hi all,
how to recursively search for a list of keywords in a given directory??

for example:
suppose i have kept all the keywords in a file called "procnamelist" (in separate line)
and i have to search recursively in a directory called "target/dir"

if i am not doing recursive search then the below command is working.


Code:
fgrep -if  procnamelist target/dir/*

but when i am using the below command for the recursive search, its throwing error as "bad option -r"


Code:
fgrep -rif  procnamelist target/dir/*

or


Code:
fgrep -Rif  procnamelist target/dir/*



not working.

please help .Linux

Last edited by Franklin52; 05-14-2012 at 04:24 PM.. Reason: Please use code tags for data and code samples, thank you
Sponsored Links
    #2  
Old Unix and Linux 05-14-2012   -   Original Discussion by neelmani
glev2005's Unix or Linux Image
glev2005 glev2005 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 19 July 2014, 10:56 PM EDT
Posts: 276
Thanks: 20
Thanked 6 Times in 6 Posts
Perhaps:

Code:
cat procnamelist|while read line;do fgrep -Rif $line target/dir/*;done

Sponsored Links
    #3  
Old Unix and Linux 05-15-2012   -   Original Discussion by neelmani
neelmani's Unix or Linux Image
neelmani neelmani is offline
Registered User
 
Join Date: May 2011
Last Activity: 8 January 2013, 12:07 PM EST
Posts: 48
Thanks: 28
Thanked 0 Times in 0 Posts
@glev2005 not working i am getting same error as :fgrep: illegal option -- R
actually the r option is not there in fgrep.. any other way??
    #4  
Old Unix and Linux 05-15-2012   -   Original Discussion by neelmani
pamu's Unix or Linux Image
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 August 2017, 11:23 AM EDT
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
Bug try this one.....



Code:
while true
do
while read line
do
cat file | grep "$line" 
done<procnamelist
done

Sponsored Links
    #5  
Old Unix and Linux 05-15-2012   -   Original Discussion by neelmani
alister's Unix or Linux Image
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 978 Times in 791 Posts
Whenever you need to traverse the a filesystem but the command does not support -R/r, think find(1).


Code:
find target/dir -type f -exec grep -iFf procnamelist /dev/null {} \;

If your find is compliant with the POSIX 2004 edition or later, use + to increase efficiency:


Code:
find target/dir -type f -exec grep -iFf procnamelist /dev/null {} +



Quote:
Originally Posted by glev2005


Code:
cat procnamelist|while read line;do fgrep -Rif $line target/dir/*;done
That would have failed even if -[r|R] were supported. A line from procnamelist, which is a pattern to match, is treated as a filename argument to -f. If you put procnamelist where it belongs, after -f, the pipe and while loop serve no purpose.


Quote:
Originally Posted by pamu


Code:
while true
do
while read line
do
cat file | grep "$line" 
done<procnamelist
done
The sole reason for the existence of this thread is file system traversal. This proposal is an infinite loop that reads one file over and over and over and ...

Regards,
Alister

Last edited by alister; 05-15-2012 at 10:56 AM..
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search a file with keywords mailabdulbari Shell Programming and Scripting 7 03-10-2010 10:52 PM
Linux shortcutkey to search specific file from a list of directory?! patrick87 UNIX for Dummies Questions & Answers 7 09-07-2009 02:50 AM
How to search for keywords in subsequent lines rdhanek Shell Programming and Scripting 12 08-11-2009 05:43 AM
Search and replace words between two keywords vrrajeeb Shell Programming and Scripting 6 05-11-2005 12:15 PM



All times are GMT -4. The time now is 05:53 AM.