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!

[tags]grep, cat, bash, linux, search, text, uuencode, random[/tags]

This entry was posted in sysadmin notes and tagged . Bookmark the permalink.



4 Responses to Don’t Grep the Cat

  1. Wikke BELGIUM Mozilla Firefox Windows says:

    I see the grep the cat joke, i scroll a bit further and behold… the geek test result of a perfect 100 :-P

    Coincidence? I don’t think so :-)

    Reply  |  Quote
  2. Luke UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Yup, I can totally see a strong correlation right there. :mrgreen:

    Reply  |  Quote
  3. Rub3X UNITED STATES Mozilla Firefox Windows says:

    Always do, always will -.-

    Reply  |  Quote
  4. Robin Stamer CANADA Mozilla Linux says:

    In the social circles I frequent this practice earns a “useless use of cat award”.

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>