Cantech Knowledge Base

Your Go-To Hosting Resource

How to Use the egrep Command in Linux?

Linux’s `egrep` (Extended Global Regular Expression Print) command is a robust text-search utility that allows users to search for text in a file, matching it to a given pattern. `Egrep` is part of the grep family of commands that includes grep, and fgrep. What differentiates egrep from `grep` is that egrep applies extended regular expressions (ERE), which makes searches and searching for patterns more complex more easily.

Thus unlike grep, which implements basic regular expression (BRE) and requires special characters to be escaped, egrep simplifies pattern matching and therefore does not require an escape sequence, thus allowing users to search using advanced operators such as ` `, `?`, or `|` without the additional formatting.

Consequently, egrep is preferred for tasks that require more advanced analysis. By following this tutorial, we will learn how to use egrep in Linux, and provide syntax, examples, and ways to use egrep for more complex searching. Searching for keywords, filtering logs, or analyzing a large amount of data. Egrep is a reliable way to process text based information.

Regular Expressions in egrep

  • Regular expressions (regex) are character sequences used for pattern matching. egrep supports the following extended regular expression features:
  • Anchors (^, $): Match the start or end of a line. Example: ^error
  • Character Classes ([ ]): Match any character within brackets. Example: [a-z]
  • Repetition (*, +, ?): Specify zero or more (*), one or more (+), or zero or one (?) occurrences. Example: a*, a+, a?
  • Alternation (|): Match between multiple options. Example: error|warning
  • Grouping (( )): Group patterns. Example: (error|warning)
  • Word Boundaries (\b): Match whole words. Example: \berror\b
  • Line Boundaries (^pattern$): Match entire lines. Example: ^error$

BRE vs. ERE in grep and egrep

  • BRE (Basic Regular Expressions – grep): Requires escaping special characters.

grep ‘a\+’ file.txt

  • ERE (Extended Regular Expressions – egrep): No escaping required, making syntax simpler.

egrep ‘a+’ file.txt

Basic Syntax of egrep

  • egrep [options] ‘pattern’ [file…]
  • [options] – Modifies command behavior
  • ‘pattern’ – Defines the search pattern
  • [file…] – Specifies the target file(s)

Setting Up Files for Testing

Follow these steps to create files for testing egrep:
Navigate to your home directory:

cd

Create test files:

touch Cantech.txt Cantech.txt cantech.txt

Create directories and subdirectories:

mkdir -p dir dir1/sub_dir

Cantech.txt using nano:

nano Cantech.txt

Add the following content:

pgsql
Cantech
Hello World, Greetings from Cantech! This is file number 1
Example text, greetings from Cantech!
Example text, greetings from VULTR!
Number of data centers: 25+ locations worldwide
Number of server plans: Over 20 different plans
CPU cores: From 1 to 64 cores
RAM: From 512MB to 256GB
Storage: SSD storage up to 10TB
Average uptime: 99.9%
Save and exit.

Commonly Used egrep Options

Option Description

-i Case-insensitive search
-v Invert match (show lines that do not match)
-c Count matching lines
-n Show line numbers with matches
-l Display filenames with matches
-r Recursively search directories
-w Match whole words only
-x Match entire lines only
–color Highlight matched patterns
-A n Show n lines after match
-B n Show n lines before match
-C n Show n lines before and after match

Practical Usage of egrep

1. Basic Pattern Search

Find lines containing the keyword Cantech:

egrep 'Cantech' Cantech.txt

2. Highlight Search Results

egrep --color=auto 'Cantech' Cantech.txt

3. Case-Insensitive Search

egrep -i 'Cantech' Cantech.txt

4. Inverted Search (Exclude a Word)

egrep -v 'Cantech' Cantech.txt

5. Count Matching Lines

egrep -c 'Cantech' Cantech.tx

6. Show Line Numbers

egrep -n 'Cantech' Cantech.txt

7. Search Multiple Files

egrep 'Cantech' Cantech.txt Cantech.txt

8. Recursive Search in Directories

egrep -r 'Hello' /home/Cantech_user/dir1

9. Display Context Before & After Matches

Before match (-B):

egrep -B 2 -n 'Number' Cantech.txt

After match (-A):

egrep -A 2 -n 'Number' Cantech.txt

Before & after match (-C):

egrep -C 2 -n 'Number' Cantech.txt

Advanced egrep Usage

1. Grouping Patterns
Find lines where Example is followed by from:

egrep '(Example.*from)' Cantech.txt

2. Match Whole Words

egrep -w 'Cantech' Cantech.txt

3. Exact Line Match

egrep -x 'Cantech' Cantech.txt

Using Regular Expressions with egrep

1. Match Lines Starting with a Word (^)

egrep '^error' sample.txt

2. Match Lines Ending with a Word ($)

egrep 'success$' sample.tx

3. Match a Character Range ([ ])

Find lines containing letters a to f:

egrep '[a-f]' sample.txt

Find lines with numeric values:

egrep '[0-9]' sample.txt

4. Using Repetition (*, +, ?)

Find lines with zero or more occurrences of a:

egrep 'a*' sample.txt

Find lines with one or more occurrences of a:

egrep 'a+' sample.txt

Find lines with logs or log:

egrep 'logs?' sample.txt

Conclusion

The `egrep` command is an important utility for searching plain text in Linux environments. It is part of the `grep` family of commands that are used to search for a specific pattern in a file. `egrep` uses extended regular expressions (ERE) which allow for more complex functionality than standard `grep`, thus making it a more versatile option for searching text.

One of the major benefits of utilizing `egrep` is that it simplifies matching patterns. In basic regular expressions and `grep`, special characters must be escaped; but when using `egrep`, you may enter special character operators such as ` `, `?`, and `|` directly to form the search statement. This further improves precision in your searches and helps you filter the relevant information displayed among large datasets, log files, and structured text.

Whether you are performing a simple search, analyzing the system logs, or extracting structured data, `egrep` provides a powerful and effective solution for users in Linux. Its speed and flexibility makes `egrep` a go-to tool for system administrators, developers, and data analysts who quickly need to extract and process text accurately.

April 22, 2025