LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-07-2004, 07:07 AM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,695

Rep: Reputation: 56
Pipe command question


Hi all folks,

Kindly advise why I am not allowed to execute 'pipe' command continuously.

e.g.
$ ls -al /dev | grep hda | grep cdrom
no printout

I must perform
$ ls -al /dev | grep hda

and then
$ ls -al /dev | grep cdrom

printout comes out

TIA

B.R.
satimis
 
Old 01-07-2004, 07:20 AM   #2
MartinN
Member
 
Registered: Nov 2003
Location: Ronneby, Sweden
Posts: 555

Rep: Reputation: 30
Hi!

In your first example, you are sending the output from grep (the lines that contain 'hda') as input to next grep. So the next grep tries to find all the lines that contain 'cdrom' out of the lines that contain 'hda'. Since there are no such lines, you get no output.

Elementary, my dear Watson.

Regards
Martin
 
Old 01-07-2004, 07:24 AM   #3
GŠutama
Member
 
Registered: Jun 2003
Location: Tokyo
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
Sure you can run more "pipes" in a row. Just remember that the pipe command connects the standards out to the next standard in. ie The output of
Code:
ls -al /dev | grep hda
is fed to

Code:
grep cdrom
but since the first grep only has stuff with hda in it grep will not find anything with cdrom.

use egrep and regular expressions instead.

Code:
ls -al /dev/ | egrep hda\|cdrom
performs what I think you were looking for.

the " a | b " thing is a regular expression that means " a or b". But since "|" is the pipe command you have to throw in the "\" to tell your shell that it is the "|" character and not the pipe command.

Hope it helped.
 
Old 01-07-2004, 07:43 AM   #4
llamakc
Member
 
Registered: Nov 2003
Distribution: Debian & Ubuntu
Posts: 402

Rep: Reputation: 31
If you want to grep for multiples, use egrep:

ls -al /dev | egrep 'hda|cdrom'

HTH

EDIT: Ignore this. I really should read the earlier posts more thoroughly. DOH!

Last edited by llamakc; 01-07-2004 at 07:45 AM.
 
Old 01-07-2004, 07:46 AM   #5
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,695

Original Poster
Rep: Reputation: 56
Quote:
Originally posted by GŠutama
Sure you can run more "pipes" in a row. Just remember that the pipe command connects the standards out to the next standard in. ie The output of
Code:
ls -al /dev | grep hda
is fed to

Code:
grep cdrom
but since the first grep only has stuff with hda in it grep will not find anything with cdrom.

use egrep and regular expressions instead.

Code:
ls -al /dev/ | egrep hda\|cdrom
performs what I think you were looking for.

the " a | b " thing is a regular expression that means " a or b". But since "|" is the pipe command you have to throw in the "\" to tell your shell that it is the "|" character and not the pipe command.

Hope it helped.
Hi,

Thanks for your advice.

That is the command I am looking for.

Previously I made a mistake. I thought it will printout what is grep one by one.

B.R.
satimis
 
Old 01-07-2004, 08:00 AM   #6
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,695

Original Poster
Rep: Reputation: 56
Quote:
Originally posted by llamakc
If you want to grep for multiples, use egrep:

ls -al /dev | egrep 'hda|cdrom'

HTH

EDIT: Ignore this. I really should read the earlier posts more thoroughly. DOH!
Hi,

Thanks for your advice.

You suggest me an additional syntax.

B.R.
satimis
 
Old 01-07-2004, 08:09 AM   #7
GŠutama
Member
 
Registered: Jun 2003
Location: Tokyo
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
llamakc syntax is actually much better than mine. You can easily express more complex expressions.

Use that one.
 
Old 01-07-2004, 10:47 AM   #8
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,695

Original Poster
Rep: Reputation: 56
Quote:
Originally posted by GŠutama
llamakc syntax is actually much better than mine. You can easily express more complex expressions.

Use that one.
Noted with thanks

B.R.
satimis
 
Old 01-07-2004, 10:54 AM   #9
elluva
Member
 
Registered: Aug 2003
Location: Belguim, Ostend and Ghent
Distribution: Ubuntu
Posts: 600

Rep: Reputation: 30
still, there are some situations you don't have choise. Remember both, they come in handy...
 
  


Reply



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
metacharacters "The Pipe command mikeinanson Linux - Newbie 4 03-11-2006 12:36 PM
how to pipe and parse output of a command learnfast Linux - Newbie 2 06-15-2005 04:55 AM
Question about writing (char)0 to a pipe. karlan Programming 1 06-05-2005 04:23 PM
how to: a pipe command? youngstorm Linux - Newbie 2 02-02-2005 10:52 AM
Command-line paranoia: Converting a pipe to stdin asciimonster Linux - General 3 06-28-2004 04:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:00 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