LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to pass the result of a command to another command (like grep) (https://www.linuxquestions.org/questions/programming-9/how-to-pass-the-result-of-a-command-to-another-command-like-grep-735586/)

desb01 06-25-2009 11:05 AM

How to pass the result of a command to another command (like grep)
 
Hi,

I would like to pass the following command result to another command :

Code:

$ grep -ri 30855 *.xml | cut -d ':' -f 1
0672.xml
0674.xml
0675.xml
0678.xml
0679.xml
0681.xml
0682.xml

So I would like to grep the word name in each of those file.

I tried
Code:

grep -ri 30855 *.xml | cut -d ':' -f 1 | grep name
without any success.

What do I do wrong ?

Thank you.

irey 06-25-2009 11:29 AM

I'm not sure I understood your intentions, but if you're trying to use the output of the first command as a list of files for the second grep, then you should include the list of files in the command line, not pipe it into grep.

Code:

grep name `grep -ri 30855 *.xml | cut -d ':' -f 1`
Note that this example uses a backward apostrophe (ascii code 96).

Hko 06-25-2009 11:36 AM

Or this alternative:
Code:

grep -ri 30855 *.xml | cut -d ':' -f 1 | xargs grep name

Trumpen 06-25-2009 11:42 AM

Here is a slightly different way to solve the problem:

Code:

grep -ri "30855\|name" *.xml | cut -d: -f1 | uniq -d
This way we read the files only once and in addition we avoid the problem with filenames containing white spaces.

desb01 06-25-2009 12:09 PM

Thank you all.

irey and Hko got it right, this is what I was trying to do.

Trupen, I don't know why but your solution didn't work.


All times are GMT -5. The time now is 08:12 PM.