Visit Our UNIX and Linux User Community


Problem while using grep (multi-level) with the space-separated filepath.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem while using grep (multi-level) with the space-separated filepath.
# 1  
Old 08-26-2009
Question Problem while using grep (multi-level) with the space-separated filepath.

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
# 2  
Old 08-26-2009
edited.

Last edited by cabrao; 08-26-2009 at 07:47 AM..
# 3  
Old 08-26-2009
Code:
#!/bin/sh
temp=/tmp/aux.$$
grep -l 'patter1' /tmp/pro/* >$temp
while read path
do
  grep -l 'patter2' "$path"
done<$temp
rm -f $temp

# 4  
Old 08-27-2009
works great!! thanks a ton!! Smilie

---------- Post updated at 10:02 AM ---------- Previous update was at 09:56 AM ----------

Any idea why a path like "/home/user/new\ folder" doesn't work fine with grep when I populate this value from cat?

I mean, say a file (file.txt) had "/home/user/new\ folder" in it and in the shell script if I did the following:
path=`cat file.txt`
then the following grep command fails to give me the correct results:
/bin/egrep -l "pattern" "$path"

Any idea why that happens although I've quoted $path.

Previous Thread | Next Thread
Test Your Knowledge in Computers #191
Difficulty: Easy
In Python, the tuple data type is immutable and cannot contain mixed types.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help on For Loop to pass space separated value as one value

Hi, I am having a file say list1 with a output like below jun 12 18:23 may 20 18:23 Now i want to pass the above two values into for loop,I have written a script like this. #!/bin/bash a=`cat list1` for i in $a do echo "HI $i" done expected output: HI jun 12 18:23 (3 Replies)
Discussion started by: sumanthupar
3 Replies

2. UNIX for Dummies Questions & Answers

How convert space separated list to matched columns?

Hi I have been racking my (limited) brains to get this to work without success I have a file output which is a list of lists - ie a single column of data that is separated by space into sub lists below - I need to both split this so that each list is in a separate column (eg tab or semicolon... (8 Replies)
Discussion started by: Manchesterpaul
8 Replies

3. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

4. Shell Programming and Scripting

How to loop through space separated values?

How do I loop thru space separated values in a variable? I hate to use very complicated counter increment logic for this kind of simple problem. Expected result(using ksh) $>echo "aaa bbbb cccc" | <looping code here> var=aaa var=bbbb var=cccc $>echo "aaa bbbb cccc" | while IFS=" "... (12 Replies)
Discussion started by: kchinnam
12 Replies

5. Shell Programming and Scripting

Conditional Multi-Line Grep Problem

Hi, I have a very large file I want to extract lines from. I'm hoping Grep can do the job, but I'm running into problems. I want to return all lines that match a pattern. However, if the following line of a matched line contains the word "Raw" I want to return that line as well. Is this... (3 Replies)
Discussion started by: redbluefish
3 Replies

6. Shell Programming and Scripting

Multi level sorting script

I want to sort like below Suppose few lines in a file is like this systemid:ABC messagedestination:batchxpr replytoqname: myca systemid:BCD messagedestination:realtime replytoqname: myca systemid:ABC messagedestination:realtime replytoqname: eac systemid: BCD messagedestination:mqonline... (1 Reply)
Discussion started by: srkmish
1 Replies

7. Homework & Coursework Questions

Help with Simple Multi-Level Makefile (Extremely New at Makefile)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Basically, the prompt is make a makefile with various sub makefiles in their respective subdirectories. All code... (1 Reply)
Discussion started by: Tatl
1 Replies

8. Homework & Coursework Questions

Four-Level multi-paging on x86 system with 64 bit addressing

1. The problem statement, all variables and given/known data: Hi all, I've got a huuuuuuge problem with understanding this new concept of multi-paging. I really tried to research but i could not find anything significant. I've been trying to understand this for 4 days and i cannot. The question... (0 Replies)
Discussion started by: snowboarder
0 Replies

9. Shell Programming and Scripting

Passing space separated value to a function - error

Taking inputs in the script which is space separated and passing this to a function and i have assigned like below And then when I use for loop for the inputs i got from the user, it is taking only the first argument. Enter Names : Bala Sundar Sridhar read names namesCheck $names function... (7 Replies)
Discussion started by: balamv
7 Replies

Featured Tech Videos