315

Can anyone tell me the technical differences between grep, egrep, and fgrep and provide suitable examples?

When do I need to use grep over egrep and vice versa?

1

5 Answers 5

337
  • egrep is 100% equivalent to grep -E
  • fgrep is 100% equivalent to grep -F

Historically these switches were provided in separate binaries. On some really old Unix systems you will find that you need to call the separate binaries, but on all modern systems the switches are preferred. The man page for grep has details about this.

As for what they do, -E switches grep into a special mode so that the expression is evaluated as an ERE (Extended Regular Expression) as opposed to its normal pattern matching. Details of this syntax are on the man page.

-E, --extended-regexp
Interpret PATTERN as an extended regular expression

The -F switch switches grep into a different mode where it accepts a pattern to match, but then splits that pattern up into one search string per line and does an OR search on any of the strings without doing any special pattern matching.

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

Here are some example scenarios:

  • You have a file with a list of say ten Unix usernames in plain text. You want to search the group file on your machine to see if any of the ten users listed are in any special groups:

    grep -F -f user_list.txt /etc/group
    

    The reason the -F switch helps here is that the usernames in your pattern file are interpreted as plain text strings. Dots for example would be interpreted as dots rather than wild-cards.

  • You want to search using a fancy expression. For example parenthesis () can be used to indicate groups with | used as an OR operator. You could run this search using -E:

    grep -E '^no(fork|group)' /etc/group
    

    ...to return lines that start with either "nofork" or "nogroup". Without the -E switch you would have to escape the special characters involved because with normal pattern matching they would just search for that exact pattern;

    grep '^no\(fork\|group\)' /etc/group
    
4
  • 24
    Some systems don't have egrep or fgrep; -E and -F are standard. There are actually small incompatibilities in egrep: it treats { slightly differently. Commented Aug 3, 2011 at 23:59
  • 5
    fgrep uses the Aho-Corasick backend, while grep uses a modified version of Commentz-Walter. This means that grep has a worst case O(mn) complexity while fgrep is at worst O(m+n). Commented Jun 26, 2015 at 20:53
  • 2
    Gilles "au contraire mon capitan", some systems (older perhaps) may not have -F option to standard grep but have a fgrep, some ppl complain about that on the original thread I found about using grep for special characters Commented Oct 25, 2015 at 9:36
  • I recently happened upon an Android system on which egrep and grep -E were handled by different executables, BusyBox and BSD grep. The two variants had behavioral differences for non-Posix regex constructs, such as \s. This led to much confusion...
    – Lii
    Commented Sep 4, 2019 at 11:42
35

From man grep:

 egrep is the same as grep -E.
 fgrep is the same as grep -F.  Direct invocation as either egrep  or  
  fgrep  is  deprecated,  but  is provided to allow historical applications
  that rely on them to run unmodified.

You use fgrep or grep -F if you don't want the grepped string to be interpreted as a pattern.

You use egrep or grep -E if you need to use an extended regexp.

0
17

egrep and fgrep are basically equivalent to grep -E and grep -F (respectively):

   -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.)

Error messages might differ though.

2
  • 4
    ok, but what is an extended regular expression? It doesn't really explain below. A few examples would be nice... Commented Aug 26, 2014 at 21:05
  • @isomorphismes: This wikibooks link explains it nicely...
    – zb226
    Commented Feb 18, 2016 at 9:38
8

From "man grep":

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.

1
  • 2
    The question asks for a suitable example. Commented Aug 26, 2014 at 21:05
3

If you search for a verbatim string, and want to be sure that the string you passed is interpreted verbatim (i.e. without risking that say a dot or question mark will be interpreted as something else) use fgrep or egrep -F.

As for the difference between egrep and grep, I think that egrep is most of the time the thing you want. The GNU manual site lists the differences between grep and egrep which seems to lay in the syntax: some things require slashes while other do not. I think that egrep is more "compatible" with perl regexps and javascript regexps so it is easier to me to use egrep.

Btw. I really encourage you to give ack a try - it supports PREGs, and has more useful defaults (i.e. coloring, skipping probably-not-what-you-want directories like .svn, ability to follow symlinks, and it's a bit easier to type ack than grep -E).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .