Recursive grep | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Recursive grep

UNIX for Dummies Questions & Answers


Tags
grep, grep recursive, recursive grep

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-16-2008
upstate_boy upstate_boy is offline
Registered User
 
Join Date: May 2008
Last Activity: 22 March 2010, 9:47 PM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Recursive grep

Hello,

First time post - I have no formal unix training and could use some help with this. I have a list of strings in File1 that I want to use to do a recursive search (grep) under a specific directory.


Here is an example of the string I need to search:

/directory/dire ctory/directory/dire ctory/filename

I'm trying to illustrate that the string is a full directory path of a file where some of the directories have spaces in their names.

I then have the following script:

for h in `cat file1`; do grep -rl "$h" /../../../../../ >> /../../file2 ; done

So, I'm trying to say for each string in file1, do a recursive grep in the specified directory and print the results to file2.

The problem (I think) I'm running into is the format of the string I'm searching, the cat I'm doing is treating the spaces as escapes which throws the grep off. I've tried putting the string in single and double quotes but it's still not working.

Sorry for the lack of technical terminology - I hope I was clear enough.

If anyone can offer any help on making it work with what I have or a simpler alternative to what I have, it would be a great help.

Thanks - upstate boy
Sponsored Links
    #2  
Old 05-16-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 August 2014, 11:31 PM EDT
Location: NM
Posts: 10,174
Thanks: 273
Thanked 784 Times in 733 Posts

Code:
find /path/to/search/in -type f | \
while read filename
do
       grep -f /path/to/strings.txt $filename
done  > /home/upstate_boy/results.txt

grep -f <file> means to use the strings in <file> as search strings for grep.
The done > filename part writes the output of the loop to filename

Last edited by jim mcnamara; 05-16-2008 at 09:07 AM..
Sponsored Links
    #3  
Old 05-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
The relative path to file2 seems wrong; the output redirection is relative to the current directory, not the directory of the file you are grepping.

The relative pat you are grepping seems wrong too; /../ is equivalent to / is equivalent to /../../../../../

The backticks in the for loop are what are splitting up stuff on whitespace. Use a construct which is less sensitive to spacing issues, or use proper quoting.


Code:
for h in "`cat file1`"; do grep -rl "$h" pathtodir >>file2; done

or


Code:
while read h; do grep -rl "$h" pathtodir >>file2; done<file1

    #4  
Old 05-16-2008
upstate_boy upstate_boy is offline
Registered User
 
Join Date: May 2008
Last Activity: 22 March 2010, 9:47 PM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you both for the replies. I don't think I'm executing your suggestions correctly, I've tried all 3.

Jim,

I'm definately confused by which files go where when I read yours.

assume:
strings.txt = file with strings I want find
results.txt = output file of search results

I am trying:

find /directory/I/want to/search/ -type f | \
while read results.txt
do
grep -f strings.txt $results.txt
done

When I use this, I get:

read: `results.txt': not a valid identifier

era,

I didn't get any errors with your suggestions but strings I'm searching are still being broken up, meaning the spaces or '/' in the strings are being handled as breaks turning 1 string into several small strings that are each getting searched.

A better example of what I was originally trying to do is:

for h in `cat strings.txt`; do grep -rl "$h" /directory/path/I want/to/search/ >> /home/directory/results.txt ; done

using /../../ in my original post was not the best choice on my part when they are the equivalent of back ticks.


I'm going to continue to fiddle with all the suggestions, if any further guidance can be offered it would be a great help.


Thanks upstate boy
Sponsored Links
    #5  
Old 05-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
The variable in Jim's example can't be named results.txt; just change it to e.g. "file" and you should be fine.

Anything with significant spaces in it should be double-quoted.
Sponsored Links
    #6  
Old 05-16-2008
upstate_boy upstate_boy is offline
Registered User
 
Join Date: May 2008
Last Activity: 22 March 2010, 9:47 PM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
I've changed it to:

find /directory/I/want to/search/ -type f | \
while read file
do
grep -f strings.txt $results.txt
done

Results now are:

grep: .txt: No such file or directory

Can someone spell out exactly how I should have it based on the example I've been using?

Thanks upstate boy
Sponsored Links
    #7  
Old 05-16-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 20 August 2014, 11:31 PM EDT
Location: NM
Posts: 10,174
Thanks: 273
Thanked 784 Times in 733 Posts
See edit above, in red.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Recursive Grep with replace yale_work Shell Programming and Scripting 2 02-23-2012 12:41 PM
recursive grep big123456 UNIX for Advanced & Expert Users 3 10-12-2011 06:11 AM
recursive grep output f_o_555 UNIX for Dummies Questions & Answers 5 10-02-2008 05:37 AM
recursive grep issue Mace UNIX for Dummies Questions & Answers 1 08-11-2006 07:39 AM
recursive GREP ? alan UNIX for Dummies Questions & Answers 3 08-22-2003 03:15 AM



All times are GMT -4. The time now is 04:00 PM.