You are not logged in.

#1 2009-05-24 15:49:54

neuwerld
Member
From: Sweden
Registered: 2009-03-13
Posts: 75
Website

Cat+Grep or Grep on itś own[SOLVED]

Hi!
Im starting this post by saying im sorry if the topic is confusing.
I would just like to know what the difference between to commands are.

cat /var/log/dmesg.log | grep CPU

or

grep "CPU" /var/log/dmesg.log

This was maybe an easy example on what I am wondering, what I can get out from it this two commands will get me the same result.
Why should I use the cat command with the grep command?
Im kinda new on Linux and would like some guidence smile

If there is someone thatś up for it you are welcome to explain some examples for me so that I understand it further.

/Neuwerld

Last edited by neuwerld (2009-05-25 05:04:32)

Offline

#2 2009-05-24 15:52:48

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Cat+Grep or Grep on itś own[SOLVED]

There is no difference. If you don't specify a filename when using grep, it reads from standard input (i.e. you type stuff and it searches through what you typed). Otherwise, it searches the file.

cat simply outputs the file's contents. The | is a pipe — it takes the output from the first command and gives it to the second command as input.

Offline

#3 2009-05-24 16:05:11

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,392
Website

Re: Cat+Grep or Grep on itś own[SOLVED]

Using cat first launches an extra process so is less efficient.

Offline

#4 2009-05-24 16:26:39

bwalk
Member
Registered: 2007-03-21
Posts: 177

Re: Cat+Grep or Grep on itś own[SOLVED]

Allan wrote:

Using cat first launches an extra process so is less efficient.

Called `cat abuse'. Poor cats...

Offline

#5 2009-05-24 16:28:49

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Cat+Grep or Grep on itś own[SOLVED]

Miow big_smile

Seriously, leave teh kitties alone!


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#6 2009-05-24 16:31:05

moljac024
Member
From: Serbia
Registered: 2008-01-29
Posts: 2,676

Re: Cat+Grep or Grep on itś own[SOLVED]

Is this ever going to get published ?


The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
--------------------------------------------------------------------------------------------------------------
But if they tell you that I've lost my mind, maybe it's not gone just a little hard to find...

Offline

#7 2009-05-24 20:20:22

neuwerld
Member
From: Sweden
Registered: 2009-03-13
Posts: 75
Website

Re: Cat+Grep or Grep on itś own[SOLVED]

Thanks for the response!
Then it's better to just use the grep command alone.
But isn't there anytime that there is better use cat+grep?

Offline

#8 2009-05-24 20:33:11

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: Cat+Grep or Grep on itś own[SOLVED]

neuwerld: It's better if you want to use any of cat's more interesting features. See cat(1).

Offline

#9 2009-05-24 20:53:25

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Cat+Grep or Grep on itś own[SOLVED]

Peasantoid wrote:

neuwerld: It's better if you want to use any of cat's more interesting features. See cat(1).

Just what I was about to say.

Also, if you have a particularly nasty list of files and/or a particularly complicated set of flags to pass to grep, you may want to separate them logically for the sake of making it easier to read.  If this is an issue, you'll probably be writing a shell script, not entering commands at a prompt.

For most cases, grep by itself is perfectly sufficient; however, you will see the cat | grep invocation from time to time.  I'm guilty of abusing cats myself smile

Offline

#10 2009-05-24 21:38:56

lastchancetosee
Member
From: Berlin
Registered: 2009-02-19
Posts: 131

Re: Cat+Grep or Grep on itś own[SOLVED]

Using cat first launches an extra process so is less efficient.

Which would probably make it contrary to the arch way, but seriously - in this day and age - is the extra number-crunching induced by piping cat to grep really an issue?
Unless you are writing a program where speed and efficiency are important I'd say forget about the waste.

Both work, depending on context one might be preferable to the other. "cat | grep" intoduces you to the awesome power of piping ;-) (but I still use grep <keyword> <file>, mostly).


My ship don't crash! She crashes, you crashed her!

Offline

#11 2009-05-24 21:47:58

neuwerld
Member
From: Sweden
Registered: 2009-03-13
Posts: 75
Website

Re: Cat+Grep or Grep on itś own[SOLVED]

Ok then I think I have it figured out smile
Maybe in the future I will learn more about it practically!

Thanks everyone!

Offline

Board footer

Powered by FluxBB