LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pipe command question (https://www.linuxquestions.org/questions/linux-newbie-8/pipe-command-question-132351/)

satimis 01-07-2004 07:07 AM

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

MartinN 01-07-2004 07:20 AM

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

GŠutama 01-07-2004 07:24 AM

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.

llamakc 01-07-2004 07:43 AM

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!

satimis 01-07-2004 07:46 AM

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

satimis 01-07-2004 08:00 AM

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

GŠutama 01-07-2004 08:09 AM

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

Use that one.

satimis 01-07-2004 10:47 AM

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

elluva 01-07-2004 10:54 AM

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


All times are GMT -5. The time now is 01:14 PM.