Recursive grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Recursive grep
# 8  
Old 05-17-2008
Jim, thanks for spelling it out for me. I got it to work but it's not producing the results I need. The results going to the results.txt are the actual contents of the files, and they are not matching my string fully. I need the files that contain the strings I'm searching - which I realize I didn't state clearly initially.

The 2 scripts I've come up with are:

for h in `cat strings.txt`; do echo "**$h**" ; grep -rl $h /path/to/search/ >> results.txt ; done

and

for h in `cat strings.txt`; do find /path/to/search/ -name \*xml -exec grep -l "$h" {} \; >> results.txt ; done


The grep and the find are working fine, it's the `cat` that is giving me trouble. The strings in strings.txt are getting broken up into smaller strings - which I verified by putting that echo in on the grep script.

Example of string in strings.txt is:

/sample/string in/strings file/title.jsp

The cat (and grep -f) is breaking it up into:

/sample/string
in/strings
file/title.jsp

I've tried putting the string in strings.txt in both single and double quotes:

"/sample/string in/strings file/title.jsp"
'/sample/string in/strings file/title.jsp'

and have also tried putting single and double quotes in the scripts:

for h in "`cat strings.txt`"; do echo "**$h**" ; grep -rl "$h" /path/to/search/ >> results.txt ; done

And the echo still shows the string being split into 3 smaller strings.




Thanks upstate boy

Last edited by upstate_boy; 05-17-2008 at 11:16 AM..
# 9  
Old 05-17-2008
Try to change the field separator in your script:

Code:
OIFS=$IFS
IFS=""

# Do your stuff here

IFS=$OIFS

# 10  
Old 05-17-2008
Thanks for the suggestion Franklin52. I do see the echo showing the full string now, but the results of the grep are off.

If I do the grep manually - I get 3 files returned which is correct.

If I use my script - I get 1588 files returned.

Script now:

OIFS=$IFS
IFS=""

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

IFS=$OIFS
# 11  
Old 05-17-2008
Why don't you use the -f option?

Code:
grep -rl -f strings.txt /path/to/search/*

# 12  
Old 05-17-2008
I tried grep -rl -f strings.txt /path/to/search/* > result.txt

Same problem, the string in strings.txt is being split up:


/sample/string in/strings file/title.jsp

I'm guessing it is being split into these 3 strings:

/sample/string
in/strings
file/title.jsp

I know that if I do this grep, I get only 3 results as opposed to the 1588 results I get with the grep -rl -f strings.txt method.

grep -rl "/sample/string in/strings file/title.jsp" /path/to/search/*

Thanks upstate boy

Last edited by upstate_boy; 05-17-2008 at 04:51 PM..
# 13  
Old 05-17-2008
Could the string in string.txt actually contain something else than plain spaces? Can you inspect it with a hex dump tool (xxd, od, what have you)?
# 14  
Old 05-17-2008
Era - I'm not sure how to inspect in the way you are asking but I've deleted the stings.txt and created a new one with vi adding the string back - no copy/paste. When trying grep -rl -f strings.txt I'm still seeing the same behavior as already described.

Thanks upstate boy
 
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