Skip to content

fgrep vs grep

01/05/2012

When you’re a chisler and you first get onto a *nix box you quickly find the magic that is the command grep. It’ll search through the files you specify and find the occurrence of the pattern you are looking for like your very own google. Most of us leave it at that. Some of us might go onto find out about the various switches that come with grep like -v, -c or -h and if you’re really unfortunate you may go onto use it’s regular expression features. If you don’t know what regular expressions are count yourself lucky and move along, don’t google it, move along, trust me if you like your hair on your head and not clumps in your hands, move along.

If you’re a regular user of regular oul’ grep pattern matching then you are using the wrong tool. To my mind if you are just using grep for pattern matching then you should really be using fgrep.

fgrep means fast grep I believe I read somewhere. It cuts out the regular expression stuff and only does pattern matching which speeds up it’s operation. We had scripts in my workplace that used grep to search through huge files. The scripts were a real bottle neck in the stuff we were doing. We went through the scripts and replaced grep with fgrep and the improvement was remarkable.

I did a rough test to see how much quicker one was than another. Yesterday one of our boxes created 32,174,185 records. So I ran the following 2 searches against them as a rough way of comparing performance.

$ date;fgrep -c ,601,600, PP.20120430.* > /dev/null;date
Tue May 1 13:27:38 IST 2012
Tue May 1 13:29:42 IST 2012
$ date;grep -c ,601,600, PP.20120430.* > /dev/null;date
Tue May 1 14:03:09 IST 2012
Tue May 1 14:07:06 IST 2012

As you can see fgrep took 2 minutes 4 seconds whilst grep took 3 minutes 57 seconds. So fgrep was ~47% faster than grep. This is significant especially if you have to do it repeatedly as part of a call in a script.

Do a man on fgrep to find out more.

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: