Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm just learning about the grep family of programs, and it seems to me that egrep is strictly superior to grep - it can do everything that grep can do, but more. Am I wrong? Why not just use egrep every time?

EDIT: I know that grep -E is the same as egrep. I want to know why grep -E isn't the default mode for grep, seeing as it only expands its usability and has no apparent drawbacks.

share|improve this question

4 Answers 4

up vote 8 down vote accepted

Technical answer: traditionally, egrep used a deterministic finite automaton (DFA) internally while grep used a non-deterministic finite automaton (NFA). These days, GNU grep and egrep take a hybrid NFA/DFA approach.

According to Friedl's book Mastering Regular Expressions, to discover if your egrep (for example) has an NFA engine or if it has a DFA engine try:

echo =XX========================================= | egrep 'X(.+)+X'

Freidl (p.147) says:

If it takes a long time to finish, it's an NFA ... If it finishes quickly, it's either a DFA or an NFA with some advanced optimization. Does it display a warning message about a stack overow or long match aborted? If so, it's an NFA.

Friedl describes the NFA engine as "regex-directed" and the DFA as "text-directed". The details of the distinction are described from p.153 of his book onwards.

The consequence is that there are some pattern/text combinations that are matched more quickly by a DFA and some that are matched more quickly by an NFA. Also, the way you write a regex for an NFA can have a significant effect on the speed of matching. Often, a DFA will be faster, but DFAs do not support lazy matching, they match differently in some cases, they cannot do look-around expressions or back-references, and they omit some other features compared to NFAs.

According to Freidl, GNU grep uses a DFA when possible and reverts to an NFA when back-references are used.

share|improve this answer
    
In the code snippet, is it supposed to be 'egrep' or 'grep'? By what you've written, "egrep = DFA", so it ought not to take a long time to finish, as "egrep != NFA". –  Nevin Williams May 29 '13 at 19:20
    
I think Friedl means that the example shows if egrep in this case (or grep, if you replaced egrep with grep) is using a DFA or NFA. Given, as other answers have noted, that different implementations of the same tool can use different regex engines, the fact that a given tool traditionally used a particular engine doesn't mean that a particular implementation of that tool necessarily uses the same engine type. –  Simon May 29 '13 at 20:34

The "family" are each just shortcuts to different grep options (from man grep):

In addition, three variant programs egrep, fgrep and rgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F. rgrep is the same as grep -r. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.

   -E, --extended-regexp
          Interpret  PATTERN  as  an  extended   regular
          expression (ERE, see below).  (-E is specified
          by POSIX.)

   -F, --fixed-strings
          Interpret PATTERN as a list of fixed  strings,
          separated  by  newlines, any of which is to be
          matched.  (-F is specified by POSIX.)

   -R, -r, --recursive
          Read   all   files   under   each   directory,
          recursively;  this  is  equivalent  to  the -d
          recurse option.
share|improve this answer

egrep is just a shortcut for grep -E which allows the usage of extended regular expressions. Check out the man page for egrep– it will pull up the man page for the "family" of pattern search functions like grep, egrep, fgrep, etc.

As for usage, if you use extended regular expressions, then typing egrep might be faster that typing in grep -E all the time.

share|improve this answer

The drawback to egrep is that its regex is a little more complex and less convenient if you don't need the extra capability. Sometimes, more power isn't better if it's not as simple and easy to use.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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