It has come to my attention that some of you whippersnappers out there are greping the cat. I hope you know that greping the cat is totally not appropriate. I would say it’s just plain wrong, and you all know it! So stop it! It’s wasteful!
Let me demonstrate this idea. First let’s create a file with a 1000 random lines of text:
$ head -n 1000 /dev/random | uuencode - > test.tx
Now let’s look for some string within that file. I will be looking for 13 cause it’s the 13th of December today, and there is a fairly good chance I will get few hits in there. First let’s look by greping the cat:
$ time cat test.txt | grep -c 13
It’s not bad, but totally wasteful. Grep is fully capable of reading files on it’s own, so it is much more desirable to do it this way:
$ time grep -c 13 test.txt
Look at the numbers yourself. You have a totally visible performance gain when you do it the correct way. So remember kids: don’t grep the cat!
[tags]grep, cat, bash, linux, search, text, uuencode, random[/tags]