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 4 Weeks Ago 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
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-15-2008
Registered User
 

Join Date: May 2008
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-15-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,289
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 06:07 AM.
Reply With Quote
  #3 (permalink)  
Old 05-15-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #4 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #5 (permalink)  
Old 05-16-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
  #6 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: May 2008
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #7 (permalink)  
Old 05-16-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,289
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
See edit above, in red.
Reply With Quote
  #8 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: May 2008
Posts: 8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
  #9 (permalink)  
Old 05-17-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,387
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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,387
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
  #15 (permalink)  
Old 05-17-2008
ninjaslim's Avatar
Registered User
 

Join Date: May 2008
Location: North Brunswick, NJ
Posts: 10
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Do you have any hex dump tools at your disposal?
Reply With Quote
Google UNIX.COM
Reply

Tags
grep, grep recursive, recursive grep

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script rsync ftp scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script