Home > Text Processing & Search > egrep

egrep: Search Text with Extended Regular Expressions

egrep is a variant of the grep command that, by default, uses Extended Regular Expressions (ERE) to search for patterns in files and print matching lines. It provides the same functionality as grep -E, allowing you to use metacharacters like +, ?, |, and () directly without escaping, making it useful for complex pattern matching.

Overview

egrep is used to find lines containing specific patterns in text files. Unlike the standard grep, egrep natively supports Extended Regular Expressions (ERE), enabling more flexible and powerful pattern matching.

Key Features

  • Native support for Extended Regular Expressions (ERE)
  • Functionally identical to grep -E
  • Control over search and output using various options
  • Filtering output of other commands when used with pipes (|)

Common Options

Here are some of the most frequently used options with the egrep command.

Search Control

Output Format

Generated command:

Try combining the commands.

Description:

`egrep` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Here are various examples of how to use the egrep command.

Basic Extended Regular Expression Search

egrep 'apple|banana' fruits.txt

Searches for lines containing 'apple' or 'banana' in a file.

Case-Insensitive Search

egrep -i 'error' logfile.log

Searches for lines containing 'error' (case-insensitive) in a log file.

Output Lines Not Starting with a Comment (#)

egrep -v '^#' config.ini

Filters out commented lines from a configuration file to view actual settings.

Recursive Search in a Specific Directory

egrep -r 'function_name' ./src

Searches for 'function_name' in all files within the 'src' subdirectory of the current directory.

Search with Line Numbers

egrep -n 'keyword' document.txt

Searches for 'keyword' in a document file and outputs the line number along with the matching line.

Search for Specific Processes in Process List

ps aux | egrep 'apache|nginx'

Filters the output of `ps aux` to find processes related to 'apache' or 'nginx'.

Tips & Notes

Useful tips and points to consider when using egrep.

Relationship with grep -E

On most modern Linux systems, egrep is a symbolic link or alias to grep -E. Therefore, using grep -E is more common and recommended.

  • egrep == grep -E
  • Using egrep or grep -E is more convenient than standard grep for complex regular expressions.

Performance Considerations

When searching for fixed strings rather than regular expressions, using fgrep (or grep -F) can be more performant. fgrep does not have the overhead of parsing regular expressions.

  • For fixed string searches: Use fgrep (or grep -F)
  • For regular expression searches: Use egrep (or grep -E)

Regular Expression Escaping

In standard grep, metacharacters for extended regular expressions like +, ?, |, and () must be escaped with a backslash (\). However, egrep allows you to use them directly without escaping.

  • In egrep, use directly like (a|b)+
  • In standard grep, escaping is required, e.g., \(a\|b\)\+


Same category commands