3

What are the differences between grep, fgrep and egrep? What are some examples of using them differently?

4

See man grep:

The variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated, but are provided for backward compatibility.

And

Pattern Syntax
   -E, --extended-regexp
          Interpret PATTERNS as extended regular expressions (EREs, see below).

   -F, --fixed-strings
          Interpret PATTERNS as fixed strings, not regular expressions.

   -G, --basic-regexp
          Interpret PATTERNS as basic regular expressions (BREs, see below).  
          This is the default.

   -P, --perl-regexp
          Interpret PATTERNS as Perl-compatible regular expressions (PCREs).  
          This option is experimental when combined with  the  -z  (--null-data)
          option, and grep -P may warn of unimplemented features.

  • Prefer grep -F whenever you have no pattern to match as it is a lot faster.
  • Use grep -E and especially grep -P only when Basic Regex does not fullfil your needs as the matching will be slower.
| improve this answer | |
2

What's Difference Between Grep, Egrep and Fgrep in Linux? - TecMint explains the difference well. All three do the same thing with the same code, but have different options.

egrep is the same as grep-E, interpreting PATTERN as an extended regular expression. It treats meta-characters as is and doesn't substitute them as strings like in the original grep. GNU's grep, which is used by Ubuntu, has no difference in available functionality between basic and extended syntax (unlike the original grep found in some other distros).

fgrep is the same as grep-F. interpreting PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched. It doesn't recognize regular expressions, nor any meta-characters. For searching any direct string, it's faster, so this is the version of grep which should be selected.

| improve this answer | |

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.