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 ...")
zgrep
is generally a script shipped with gzip
(see also Stephen Kitt's comment below) that grep
s 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.
fgrep
, that could perhaps stand for flat grep
.
Commented
Oct 4, 2017 at 10:46
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
.
Commented
Oct 4, 2017 at 11:02
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
.
Commented
Oct 4, 2017 at 15:43