Hi,
I've been trying to use grep to find out all the files which have two particular patterns in it (both pattern1 AND pattern2). I have a script to do the same, in which I'm getting the output of the first grep (with -l option) which contains the list of file paths and I'm trying to search for pattern2 in this file path. Things are fine as long as the file path has no space-separated path in it.
My script looks like this:
#!/bin/sh
path=`grep -l 'pattern1' /home/user1/*`
#Say the output of the same was ----> "/home/user1/new file"
grep -l 'pattern2' $path
The second grep above fails coz the value of $path is treated as 2 different paths (/home/user1/new AND file).
So I used sed to replace all " " with "\ ". For this I had to write the output to a file. When I "cat" the output of the sed formatted file as the filepath to my 2nd grep expression, it's still failing to get me the correct results.
Is my problem related to how "cat" deals with files or is there something else I must be doing?
If a file.txt has the following text in it:
/home/user1/new\ file
the following grep command fails (it consider that path as two different paths) in command-prompt too:
grep 'pattern' `cat file.txt`
Any light on this will be of great help!
Thanks
NJ