LinuxQuestions.org
Visit Jeremy's Blog.
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, 08:07 AM   #1
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,637

Rep: Reputation: 54
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, 08: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, 08: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, 08: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 08:45 AM.
 
Old 01-07-2004, 08:46 AM   #5
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,637

Original Poster
Rep: Reputation: 54
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, 09:00 AM   #6
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,637

Original Poster
Rep: Reputation: 54
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, 09: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, 11:47 AM   #8
satimis
Senior Member
 
Registered: Apr 2003
Posts: 3,637

Original Poster
Rep: Reputation: 54
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, 11: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


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


All times are GMT -5. The time now is 08:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration