[comp.unix.questions] egrep, fgrep, grep

billw@wolf.UUCP (03/30/87)

I have always used grep when searching expressions, and gotten along fine. So
what is the difference between grep, egrep, and fgrep? I am on a PC AT running
SCO Xenix: no man pages. But even when I do pull the man page off of a 3b2, it
just muddies the water further. Help.
-- 
Bill Wisner
{sdcsvax,ihnp4}!jack!wolf!billw
Everything is under control.

mouse@mcgill-vision.UUCP (der Mouse) (04/10/87)

In article <276@wolf.UUCP>, billw@wolf.UUCP (The Dealy Lama) writes:
> I have always used grep when searching expressions, and gotten along
> fine. So what is the difference between grep, egrep, and fgrep?

The difference is supposed to be:

grep:	workhorse, the "usual" program
fgrep:	fast version, for use when the "expression" is a constant
	  string (no regular expression support)
egrep:	extended version, fancier regular expressions, but slower and
	  chews up more memory.

In fact, of the above three there is no reason to use grep, and use
fgrep only when the string is unknown (such as an argument to a shell
script) and you don't know whether it may contain r.e. metacharacters.
Egrep is usually the fastest and generally best of the three.

The above paragraph is usually true, but if you really care I would
advise doing careful tests on your system to find out which one is
really fastest, smallest, or whatever your criterion for "best" is.

Note that there are readily-available searching programs much better
than any of the above.  Contact your nearest mod.sources archive site.

					der Mouse

				(mouse@mcgill-vision.uucp)

dce@mips.UUCP (04/18/87)

In article <741@mcgill-vision.UUCP> mouse@mcgill-vision.UUCP (der Mouse) writes:
>In fact, of the above three there is no reason to use grep, and use
>fgrep only when the string is unknown (such as an argument to a shell

This isn't completely true because not all options/features are
universally available or consistent.

The -w option of grep and the special sequences \< and \> are not available
in egrep or fgrep. You could implement this in egrep, but the complexity
of the resulting expression might cause egrep to be slower (I haven't
checked it, but egrep does slow down when () and | are involved).

Another missing option is the -i (-y to some folks), which is not in
egrep.

The bm command is a great idea as far as speed goes, but it is yet
another new command with different arguments.

Very soon, a (Boyer-Moore + other improved algorithms) version of
grep/egrep/fgrep will be publicly available in mod.sources (or
whatever it will be called at that time). It isn't a complete
replacement, as you will see, but it is compatible, so you won't
have people writing shell scripts that require a non-standard command.

We have been using this at Mips for a couple of months in place of the 
standard 4.3 versions, and are offering it with our next UMIPS-BSD release.
-- 
David Elliott		{decvax,ucbvax,ihnp4}!decwrl!mips!dce

karl@ima.UUCP (04/19/87)

In article <741@mcgill-vision.UUCP> mcgill-vision!mouse (der Mouse) writes:
>In fact, of the above three there is no reason to use grep ...

(a) in versions of unix where RE matching includes \{m,n\}, grep has it and
egrep doesn't.  likewise, egrep has no way (other than enumeration) to match,
say, a double letter, which grep can do with the pattern '\([a-z]\)\1'.

(b) the pattern '^........foo' is syntactically acceptable to egrep, but will
almost certainly run out of space.  grep handles it nicely.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
(i wish vanilla grep would accept the fgrep/egrep semantics of newline!)