You are not logged in.
Pages: 1
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
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
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
Using cat first launches an extra process so is less efficient.
Offline
Using cat first launches an extra process so is less efficient.
Called `cat abuse'. Poor cats...
Offline
Miow
Seriously, leave teh kitties alone!
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
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
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
neuwerld: It's better if you want to use any of cat's more interesting features. See cat(1).
Offline
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
Offline
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
Ok then I think I have it figured out
Maybe in the future I will learn more about it practically!
Thanks everyone!
Offline
Pages: 1