egrep

file managementLinux/Unix
The egrep command is one of the most frequently used commands in Linux/Unix-like operating systems. egrep Search for patterns using extended regular expressions

Quick Reference

Command Name:

egrep

Category:

file management

Platform:

Linux/Unix

Basic Usage:

egrep [options] [arguments]

Common Use Cases

  • 1

    Complex pattern matching

    Search for complex patterns using extended regular expressions

  • 2

    Multiple pattern search

    Search for multiple patterns in a single command using alternation

  • 3

    Code analysis

    Find and analyze code patterns across multiple files

  • 4

    Log file parsing

    Extract specific information from log files using powerful regex

Syntax

egrep [options] pattern [file...]

Options

Option Description
-i Ignore case distinctions in patterns and data
-v Select non-matching lines
-n Display line numbers alongside the matched lines
-c Count the number of matching lines instead of displaying them
-l Print only the names of files containing matches
-r, -R Recursively search through directories
-w Match only whole words
-A num Print num lines of trailing context after matching lines
-B num Print num lines of leading context before matching lines
-C num Print num lines of context around matching lines

Examples

How to Use These Examples

The examples below show common ways to use the egrep command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

# Basic Examples Basic
egrep "pattern" filename.txt
Search for "pattern" in filename.txt.
egrep -i "pattern" filename.txt
Search for "pattern" in filename.txt, ignoring case.
# Advanced Examples Advanced
egrep "apple|orange" fruits.txt Search for lines containing either "apple" or "orange" in fruits.txt. egrep -v "^#" config.txt Display all lines in config.txt that don't start with a # character. egrep -r "function\s+\w+\(" --include="*.js" . Recursively search for JavaScript functions in the current directory. egrep -n "(Error|Warning)" log.txt Display lines containing "Error" or "Warning" with line numbers.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

egrep is essentially identical to 'grep -E', which uses extended regular expressions that provide additional pattern matching capabilities compared to basic grep. The extended regex format allows special characters like '+', '?', '|', '()', and '{}' to be used without escaping them with backslashes. In modern implementations, egrep is often a symbolic link to grep with the -E option automatically applied. This provides backward compatibility while encouraging the use of grep with appropriate options. Common extended regex patterns in egrep: - a|b: Match either 'a' or 'b' - a+: Match one or more occurrences of 'a' - a?: Match zero or one occurrence of 'a' - a{n}: Match exactly n occurrences of 'a' - a{n,m}: Match between n and m occurrences of 'a' - (abc): Group the pattern 'abc' Egrep is frequently used for searching through code, configuration files, logs, and other text data. It's particularly useful for complex pattern matching tasks where basic regular expressions would be too limited or require excessive escaping.

Tips & Tricks

1

Use the pipe character (|) to search for multiple patterns, like "apple|orange"

2

Use the plus sign (+) to match one or more occurrences, like "go+gle"

3

Use -i for case-insensitive searches

4

Use -v to show lines that do NOT match the pattern

5

Use -r for recursive searching through directories

Common Use Cases

Complex pattern matching

Search for complex patterns using extended regular expressions

Multiple pattern search

Search for multiple patterns in a single command using alternation

Code analysis

Find and analyze code patterns across multiple files

Log file parsing

Extract specific information from log files using powerful regex

Data extraction

Pull out structured data from text files using capture groups

Related Commands

These commands are frequently used alongside egrep or serve similar purposes:

Use Cases

1

Complex pattern matching

Search for complex patterns using extended regular expressions

2

Multiple pattern search

Search for multiple patterns in a single command using alternation

3

Code analysis

Find and analyze code patterns across multiple files

4

Log file parsing

Extract specific information from log files using powerful regex

5

Data extraction

Pull out structured data from text files using capture groups

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the egrep command works in different scenarios.

$ egrep
View All Commands