Visit the LQ Articles and Editorials section
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 06-25-2009, 12:05 PM   #1
LQ Newbie
Registered: Jun 2009
Posts: 2

Rep: Reputation: 0
How to pass the result of a command to another command (like grep)


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

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

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

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

What do I do wrong ?

Thank you.
Old 06-25-2009, 12:29 PM   #2
Registered: Jun 2008
Location: Torino, Italy
Posts: 65

Rep: Reputation: 17
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.

grep name `grep -ri 30855 *.xml | cut -d ':' -f 1`
Note that this example uses a backward apostrophe (ascii code 96).
Old 06-25-2009, 12:36 PM   #3
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Or this alternative:
grep -ri 30855 *.xml | cut -d ':' -f 1 | xargs grep name
Old 06-25-2009, 12:42 PM   #4
LQ Newbie
Registered: Dec 2008
Location: Italy
Distribution: Debian & Ubuntu
Posts: 11

Rep: Reputation: 0
Here is a slightly different way to solve the problem:

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.
Old 06-25-2009, 01:09 PM   #5
LQ Newbie
Registered: Jun 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
command result as varible mkiler Programming 1 03-10-2009 11:22 AM
Help me in Grep Command + cd command in single line JeiPrakash Linux - Newbie 3 05-27-2008 05:16 AM
How to pass a result of exec command in perl to a variable??? HyperTrey Programming 3 05-23-2008 01:47 PM
python - getting command result Hewson Programming 1 08-01-2007 07:22 PM
BASH Command Result redhatnoob Programming 2 08-19-2004 07:25 PM

All times are GMT -5. The time now is 04:10 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration