Have you heard this old saw:
UNIX beginners use grep
because it's all they know about.
Intermediate users use fgrep
because the manual says it's
Advanced users use egrep
because they've tried it.
Yes, despite what the manual page says (or rather, used to say, because
nowadays, many grep
manual pages have been rewritten to
acknowledge the fact), fgrep
is usually the slowest of the
three standard grep
[I believe that fgrep
stands for "fixed grep
because it doesn't accept metacharacters. -JP
If you want to prove this to yourself, try using the
program (shown in article
to give you the average execution time of a search.
Here's the result of my
search for the string Waldo
in a large directory crowded with
saved mail files.
runtime -10 grep Waldo *
4.13u 0.83s 0:04 0+203k 21+0io 19pf+0w
runtime -10 fgrep Waldo *
5.19u 0.80s 0:05 0+195k 4+0io 2pf+0w
runtime -10 egrep Waldo *
2.61u 0.76s 0:02 0+244k 0+0io 0pf+0w
On my SPARCstation IPC, grep
managed the search in four seconds,
in five, and egrep
in only two. egrep
the least CPU time.
Just for the heck of it, let's see how some other search programs
stack up. sed
, and perl
can also emulate the
action of grep
runtime -10 sed -n '/Waldo/p' *
3.64u 1.20s 0:04 0+227k 2+0io 1pf+0w
runtime -10 awk '/Waldo/' *
4.86u 0.76s 0:05 0+279k 1+0io 0pf+0w
runtime -10 perl -ne \'print if \(/Waldo/\) \; \' *
2.94u 0.69s 0:03 0+498k 28+4io 27pf+0w
(Note that we have to escape any characters that the shell might
interpret in the perl
is faster than all but egrep
, but even sed
edges your basic grep
by a hair.
is by far the slowest - it even lost to awk
This doesn't mean that fgrep
is useless, though.
It has a
couple of handy options:
requires a line to be exactly the same as the search pattern;
takes one or many search patterns from a file.
You can sometimes exploit the fact that fgrep
regular expressions, and so using it to search for literal asterisks
or other regular expression metacharacters can save you a bit of
The time saved on the command line can be worth the slower