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:

  • How much memory do I have?
  • How to clean a toilet
  • Fingering with Finger!
  • Cat Piano
  • How hard is it to AWK it?
  • Just how geeky are you anyway?
  • BSD Licensed Code in Windows
  • Awk One Liners
  • Bash Lotto Lookup
  • Don’t Kill Freedom!

  • 3 Responses to “Don’t Grep the Cat”

    1. Gravatar Wikke BELGIUM Says: Reply to this comment

      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 -)

      Posted using Mozilla Firefox Mozilla Firefox 1.5.0.6 on Windows Windows XP
    2. Gravatar Luke UNITED STATES Says: Reply to this comment

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

      Posted using Mozilla Firefox Mozilla Firefox 2.0 on Ubuntu Linux Ubuntu Linux
    3. Gravatar Rub3X UNITED STATES Says: Reply to this comment

      Always do, always will -.-

      Posted using Mozilla Firefox Mozilla Firefox 2.0 on Windows Windows XP

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]