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 …")
grep
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 …")
Best Answer
zgrep
is generally a script shipped withgzip
(see also Stephen Kitt's comment below) thatgrep
s into compressed files (with compression formats thatgzip
recognises). Thez
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 oldgrep
(itself a standalone command to implement theg/re/p
command of the anciented
text editor). That's thegrep
for the extended regexps (ERE), as opposed to the basic regexps (BRE) understood bygrep
/sed
/ed
/vi
.Additional operators like
\{
and\<
were later added to some implementations ofgrep
but notegrep
makinggrep
on some aspects more extended thanegrep
.In the early 90s, POSIX tried to unify
egrep
andgrep
into a single command (wheregrep -E
is meant to do whategrep
did) and make the{min,max}
operator in ERE equivalent to\{min,max\}
ingrep
's REs (so not backward compatible withegrep
). It also specified the-F
option for fixed string search to replace thefgrep
utility.Today,
egrep
is not a standard command (neither isfgrep
). While most systems have one, some implementations recognise the{min,max}
operator, some don't.grep
andgrep -E
are standard. Somegrep
implementations have extra switches to recognise even more different regexp syntaxes likegrep -P
for PCRE (orperl
-like, see also thepcregrep
command shipped with the PCRE library),grep -X
for augmented regexps...And the list of operators supported by
grep
andgrep -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
(GNUzip
) understands another ancient compression format that is still in use nowadays andgzip
is found on most systems (either the GNU implementation or a clone). So you should be able to do:or:
to
grep
into gzip-compressed files. You can do the same with any other compression format provided you have access to the corresponding tool.