Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux grep -E and grep -F

Reference for grep -E (extended regular expressions) and grep -F (fixed strings): syntax differences, egrep and fgrep aliases, speed and when to use each.

grep has three pattern dialects. The default is the basic regular expression (BRE). grep -E switches to extended regular expressions (ERE), where +, ?, |, () and {} work without backslashes. grep -F switches off regular expressions altogether: the pattern is a plain string, compared byte by byte, and every character means itself.

The older commands egrep and fgrep are the same as grep -E and grep -F. They still exist but are deprecated: current GNU grep prints a warning when you use them, so write the option form in new scripts.

Terminal
grep -E 'pattern1|pattern2' file
grep -F 'literal.text[1]' file

Try it

Bash Extended regular expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Fixed strings and many patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
grep -E [OPTION]... PATTERN [FILE]...
grep -F [OPTION]... STRING  [FILE]...

All other grep options (-i, -v, -o, -r, ...) work with both; see grep.

ERE elements

Element Meaning
. Any single character.
[abc], [^abc], [a-z] Character set, negated set, range.
*, +, ? Zero or more, one or more, zero or one of the previous item.
, {n,}, {n,m} Exactly n, at least n, between n and m repeats.
a|b Alternation: either side.
( ) Grouping (and a back-reference with \1 in GNU grep).
^, $ Start and end of a line.
\<, \>, \b, \w Word start/end, word boundary, word character (GNU extensions).
[[:digit:]], [[:alpha:]], [[:space:]] POSIX character classes.

-F behaviour: the argument may hold several strings separated by newlines; a line is selected if it contains any of them. With -x the whole line must equal a string, with -w the string must be a whole word.

Returns: the same as grep: 0 for a match, 1 for none, 2 for an error.

Usage notes

  • Pick the dialect by need. Literal text, file names, URLs, code snippets with dots and brackets: -F. Anything with alternatives or counts: -E. Simple prefix/suffix tests: the default is fine.
  • Escaping in ERE. Characters such as ., *, ?, +, (, ), [, {, |, ^, $ and \ are special; match them literally with a backslash (\.) or put them in a bracket ([.]).
  • { in a BRE is literal; the repeat form is \{n\}. In ERE, is a repeat count.
  • Perl classes. \d is not part of ERE. Use [0-9] or [[:digit:]], or grep -P '\d' where PCRE support is built in.
  • Speed. -F with many patterns (-f file) uses an efficient multi-string algorithm and handles thousands of patterns quickly. Large alternations in -E are slower than the same list in a -F file.
  • Unquoted | is a shell pipe. Always quote: grep -E 'a|b'.
  • Empty pattern matches every line; an empty line in a -f pattern file does the same, a common surprise.
  • egrep/fgrep exist for old scripts. Replace them with grep -E and grep -F.