The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Recursive FTP -- here at last. Perderabo Shell Programming and Scripting 46 06-19-2008 01:40 PM
recursive grep issue Mace UNIX for Dummies Questions & Answers 1 08-11-2006 04:39 AM
recursive GREP ? alan UNIX for Dummies Questions & Answers 3 08-22-2003 12:15 AM
grep recursive directories jagannatha UNIX for Dummies Questions & Answers 8 07-24-2003 01:00 PM
Recursive FTP aslamg UNIX for Dummies Questions & Answers 1 03-08-2001 12:27 AM

Reply
 
LinkBack Thread Tools Display Modes
  #8 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: May 2008
Posts: 8
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 07:16 AM.
Reply With Quote
Forum Sponsor
  #9 (permalink)  
Old 05-17-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,561
Try to change the field separator in your script:

Code:
OIFS=$IFS
IFS=""

# Do your stuff here

IFS=$OIFS
Reply With Quote
  #10 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: May 2008
Posts: 8
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
Reply With Quote
  #11 (permalink)  
Old 05-17-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,561
Why don't you use the -f option?

Code:
grep -rl -f strings.txt /path/to/search/*
Reply With Quote
  #12 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: May 2008
Posts: 8
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 12:51 PM.
Reply With Quote
  #13 (permalink)  
Old 05-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,707
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)?
Reply With Quote
  #14 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: May 2008
Posts: 8
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
Reply With Quote
Google UNIX.COM
Reply

Tags
grep, grep recursive, recursive grep

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:52 PM.


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

Content Relevant URLs by vBSEO 3.2.0