Don’t Grep the Cat
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
94
real 0m0.388s
user 0m0.123s
sys 0m0.155s
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
94
real 0m0.189s
user 0m0.062s
sys 0m0.078s
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!
Related Posts:

December 14th, 2006 at 1:49 am (2076) [Quote]
I see the grep the cat joke, i scroll a bit further and behold… the geek test result of a perfect 100
Coincidence? I don’t think so
Posted usingDecember 14th, 2006 at 10:21 am (2090) [Quote]
Yup, I can totally see a strong correlation right there.
Posted usingDecember 17th, 2006 at 3:04 am (2137) [Quote]
Always do, always will -.-
Posted using