2

Is there an easy-to-remember rule of thumb to know what is the difference between:

  • egrep

  • zgrep

  • grep

and to know which ones are installed in my machine?

(Indeed it seems that there is some: "if GNU grep is installed, then ..., else ...")

10

zgrep is generally a script shipped with gzip (see also Stephen Kitt's comment below) that greps into compressed files (with compression formats that gzip recognises). The z is for zip (not for the pkzip compressed archive format, but for the zipping/compression of files).

egrep was a command introduced in Unix V7 in the late 70s with a new regexp algorithm and syntax compared to the old grep (itself a standalone command to implement the g/re/p command of the ancient ed text editor). That's the grep for the extended regexps (ERE), as opposed to the basic regexps (BRE) understood by grep/sed/ed/vi.

Additional operators like \{ and \< were later added to some implementations of grep but not egrep making grep on some aspects more extended than egrep.

In the early 90s, POSIX tried to unify egrep and grep into a single command (where grep -E is meant to do what egrep did) and make the {min,max} operator in ERE equivalent to \{min,max\} in grep's REs (so not backward compatible with egrep). It also specified the -F option for fixed string search to replace the fgrep utility.

Today, egrep is not a standard command (neither is fgrep). While most systems have one, some implementations recognise the {min,max} operator, some don't.

grep and grep -E are standard. Some grep implementations have extra switches to recognise even more different regexp syntaxes like grep -P for PCRE (or perl-like, see also the pcregrep command shipped with the PCRE library), grep -X for augmented regexps...

And the list of operators supported by grep and grep -E varies from one system to another. For portability, restrict to the list specified by POSIX.

On Solaris, make sure to use /usr/xpg4/bin/grep. The one in /bin is not POSIX compliant.

Various compression libraries/tools provide with zgrep, bzgrep, xzgrep scripts, none of which standard.

The only compression program that POSIX specifies is compress/uncompress which is for an ancient compression format from the early 80s that nobody uses anymore.

gzip (GNU zip) understands another ancient compression format that is still in use nowadays and gzip is found on most systems (either the GNU implementation or a clone). So you should be able to do:

gzip -d < file.gz | grep BRE

or:

gzip -d < file.gz | grep -E ERE

to grep into gzip-compressed files. You can do the same with any other compression format provided you have access to the corresponding tool.

  • 1
    There's also fgrep, that could perhaps stand for flat grep. – Satō Katsura Oct 4 '17 at 10:46
  • @SatōKatsura, yes, good point, I'll add a note for that. – Stéphane Chazelas Oct 4 '17 at 10:51
  • Some distributions ship Zutils’ zgrep instead of the gzip script. It supports more formats than gzip; it will handle uncompressed files and files compressed with gzip, bzip2, lzip or xz. – Stephen Kitt Oct 4 '17 at 11:02
  • Thanks for this excellent answer with historical details. Could you a note for noobs like me like: if you don't know what to use, take grep or grep -E , it will likely be present in your distribution (is it true or did I misinterpreted your answer?). PS: I mostly use standard Debian and Ubuntu on another computer. – Basj Oct 4 '17 at 12:39
  • @Basj, on GNU systems like Debian and derivatives, grep, egrep, fgrep will all be GNU grep and be mostly POSIX compliant (more compliant if you set the $POSIXLY_CORRECT environment variable) with many extensions, and fgrep will be exactly like grep -F and egrep exactly like grep -E. – Stéphane Chazelas Oct 4 '17 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.