Search

The egrep command

Searches lines with an extended regular expression. Equivalent to grep -E.

Syntax

egrep [opciones] patrón [archivo...]Try it in the terminal →

Most useful options

-i, --ignore-caseIgnores upper and lower case when comparing
-v, --invert-matchShows the lines that do not match
-n, --line-numberPrefixes each match with its line number
-c, --countPrints only how many lines match
-o, --only-matchingPrints only the matching part, one per line
-w, --word-regexpOnly accepts matches that are whole words

Copy-ready examples

$ egrep 'root|bin' /etc/passwdlines containing one thing or the other.
$ egrep -n 'root|beta' e.txtmatches with their line number.

Example output

2:root:x:0
3:beta.gamma
$ egrep -v '^(#|$)' config.confremoves comments and empty lines from a configuration file.
$ egrep -o '[0-9]{1,3}(\.[0-9]{1,3}){3}' access.log | sort -uevery IPv4 address in a log, without repeats.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
egrep: warning: egrep is obsolescent; using grep -ESince grep 3.8, egrep warns that it is obsolete and still runs the search. Depending on the distribution, the warning may not appear.Replace egrep with grep -E.
grep: Unmatched ( or \(The pattern has a parenthesis that is not closed. In extended expressions, a parenthesis groups instead of matching text.Close the parenthesis, or use fgrep if you are searching for the text as it is.
grep: noexiste: No such file or directoryThe file does not exist. The message says grep, not egrep, because grep is the program that runs.Check the name with ls and run it again.

Typical mistakes

  • The GNU grep manual considers egrep obsolete and recommends grep -E. POSIX removed it in 2001: in new scripts, use grep -E.
  • Without quotes, the shell interprets | and ( before egrep does. Always put the pattern in single quotes.
  • With -o, each match is printed on its own line. If you want the whole line, drop -o.

Differences from similar commands

  • grep: with -E it does the same. Without -E, | and + are plain text, so egrep saves typing -E.
  • fgrep: searches literal text. egrep, by contrast, treats | and ( as operators.
  • sed: besides searching, it transforms text. egrep only filters lines.

Related commands

FAQ

What is the difference between egrep and grep -E?

None in the result. egrep is the old name used to call grep in extended mode.

How do I match a whole word and not a part?

With -w: egrep -w root file does not return a line that only contains rootx.

Should I change my egrep scripts to grep -E?

Yes. POSIX removed it in 2001, GNU grep treats it as obsolete and since version 3.8 warns when you use it. grep -E works the same.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course