egrep Command

The egrep command searches for patterns in text using extended regular expressions. It's equivalent to grep -E and provides more powerful pattern matching capabilities than basic grep.

Syntax

egrep [OPTIONS] PATTERN [FILE...]

Description

The egrep command uses extended regular expressions (ERE) to search for patterns in files or input streams. It supports advanced regex features without requiring escape characters.

Key features:

  • Extended regular expression support
  • Alternation with | (pipe) operator
  • Quantifiers: + (one or more), ? (zero or one)
  • Grouping with parentheses ()
  • Case-sensitive and case-insensitive matching

Common Options

Option Description
-i Case-insensitive matching
-v Invert match (show non-matching lines)
-n Show line numbers
-c Count matching lines
-l Show only filenames with matches
-r Recursive search in directories
-w Match whole words only
-o Show only matching parts

Examples

Basic pattern search

egrep "error" logfile.txt

Searches for lines containing "error" in logfile.txt

Alternation (OR operator)

egrep "error|warning|critical" logfile.txt

Searches for lines containing any of the three patterns

Case-insensitive search

egrep -i "ERROR" logfile.txt

Searches for "error" regardless of case

Show line numbers

egrep -n "pattern" file.txt

Shows line numbers along with matching lines

Count matches

egrep -c "error" logfile.txt

Counts the number of matching lines

One or more quantifier

egrep "colou?r" file.txt

Matches both "color" and "colour"

Plus quantifier

egrep "[0-9]+" file.txt

Matches one or more digits

Grouping with parentheses

egrep "(http|https)://[a-zA-Z0-9.-]+" file.txt

Matches HTTP and HTTPS URLs

Word boundaries

egrep -w "test" file.txt

Matches "test" as a whole word only

Recursive search

egrep -r "function.*main" /path/to/code/

Recursively searches for pattern in all files

Show only matches

egrep -o "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" file.txt

Extracts email addresses from text

See also