LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 0
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.
 
Old 06-25-2009, 12:29 PM   #2
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 66

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.

Code:
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
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Or this alternative:
Code:
grep -ri 30855 *.xml | cut -d ':' -f 1 | xargs grep name
 
Old 06-25-2009, 12:42 PM   #4
Trumpen
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:

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.
 
Old 06-25-2009, 01:09 PM   #5
desb01
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.
 
  


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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