Recursive grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Recursive grep
# 1  
Old 05-16-2008
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
# 2  
Old 05-16-2008
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 10:07 AM..
# 3  
Old 05-16-2008
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
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
# 5  
Old 05-16-2008
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.
# 6  
Old 05-16-2008
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
# 7  
Old 05-16-2008
See edit above, in red.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Recursive grep with only certain types of files

Can I please have some ideas on how to do a recursive grep with certain types of files? The file types I want to use are *.c and *.java. I know this normally works with all files. grep -riI 'scanner' /home/bob/ 2>/dev/null Just not sure how to get it to work *.c and *.java files. (5 Replies)
Discussion started by: cokedude
5 Replies

2. Shell Programming and Scripting

Recursive find / grep within a file / count of a string

Hi All, This is the first time I have posted to this forum so please bear with me. Thanks also advance for any help or guidance. For a project I need to do the following. 1. There are multiple files in multiple locations so I need to find them and the location. So I had planned to use... (9 Replies)
Discussion started by: Charlie6742
9 Replies

3. Shell Programming and Scripting

Recursive Grep with replace

I have seen some useful infomation about recursive grep in one of the thread. Can it is possible to combine resursive grep and replace togather? Means I need to replace old server names in all the files with new server names as we are upgrading our applications. There are lots of files in... (2 Replies)
Discussion started by: yale_work
2 Replies

4. Shell Programming and Scripting

Tricky recursive removal (find with grep)

Tricky one: I want to do several things all at once to blow away a directory (rm -rf <dir>) 1) I want to find all files recursively that have a specific file extension (.ver) for example. 2) Then in that file, I want to grep for an expression ( "sp2" ) for example. 3) Then I want to... (1 Reply)
Discussion started by: jvsrvcs
1 Replies

5. UNIX for Advanced & Expert Users

recursive grep

Hi, on AIX 6.1 , in man document for grep : -r Searches directories recursively. By default, links to directories are followed. But when I use : oracle@XXX:/appli/XXX_SCRIPTS#grep -r subject *.sh It returns nothing. However I have at least one row in a file : ... (3 Replies)
Discussion started by: big123456
3 Replies

6. Shell Programming and Scripting

Preventing an endless loop with recursive grep

When finding a string in files within a directory, one can use this: grep -r "searchstring" dir/subdir/ > listofoccurrences.txt For brevity sake one can enter the intended directory and use this: grep -r "searchstring" . > listofoccurrences.txt which as I found out leads to an endless loop,... (2 Replies)
Discussion started by: figaro
2 Replies

7. UNIX for Dummies Questions & Answers

recursive grep output

I'm using this command to get a recursive grep find . -name *.i -exec grep 'blah blah' {} \; -exec ls {} \; now I would like to obtain just the list of the files and not also the line of the file. How should I change the syntax? thank you, (5 Replies)
Discussion started by: f_o_555
5 Replies

8. UNIX for Dummies Questions & Answers

recursive GREP ?

Hi! Suppose I have a directory (no symbolic links) called /WORK that contains 3 subdirectories: /A /B /C My problem is this: I want to look for a file that contains an order number. So far, I obtain what I want by doing this /home/acb% cd /WORK/A /home/acb/WORK/A% grep '093023553' *.*... (3 Replies)
Discussion started by: alan
3 Replies

9. UNIX for Dummies Questions & Answers

grep recursive directories

I am trying to locate a file or files with specific data in them. Problem is the file(s) could reside in any one of many directories. My question is. Is there a way of recursively greping directories for the file(s) with the data I am looking for. I have tried - 1. $HOME> grep 47518 | ls... (8 Replies)
Discussion started by: jagannatha
8 Replies
Login or Register to Ask a Question