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:
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.
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.
for h in "`cat file1`"; do grep -rl "$h" pathtodir >>file2; done
or
while read h; do grep -rl "$h" pathtodir >>file2; done<file1
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.
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:
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.
Problem found. I feel kind of foolish now but after so many people offered help and suggestions I feel you should see my error.
My strings.txt file had several blank lines in it prior to and after the string I wanted to search. These blank lines were what was throwing grep -f off.