home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 27.5 Extended Searching for Text with egrep Chapter 27
Searching Through Files
Next: 27.7 grepping for a List of Patterns

27.6 Fast grep Isn't

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 faster.

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 s. [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 runtime program (shown in article 39.4 ) 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, fgrep in five, and egrep in only two. egrep also used the least CPU time.

Just for the heck of it, let's see how some other search programs stack up. sed , awk , 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 command line.)

perl is faster than all but egrep , but even sed edges your basic grep by a hair. And fgrep 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: -x requires a line to be exactly the same as the search pattern; -f (27.7 ) takes one or many search patterns from a file. You can sometimes exploit the fact that fgrep doesn't understand regular expressions, and so using it to search for literal asterisks or other regular expression metacharacters can save you a bit of quoting. The time saved on the command line can be worth the slower execution speed.


Previous: 27.5 Extended Searching for Text with egrep UNIX Power Tools Next: 27.7 grepping for a List of Patterns
27.5 Extended Searching for Text with egrep Book Index 27.7 grepping for a List of Patterns

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System