Cantech Knowledge Base

Your Go-To Hosting Resource

How to Use the egrep Command in Linux?

Introduction

Linux 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 egrep Syntax

The egrep command follows the syntax given below:

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:

Cantech
Hello World, Greetings from Cantech! This is file number 1
Example text, greetings from cantech!
Example text, greetings from CANTECH!
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

The list below shows some of the most commonly used egrep command 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 Command in Linux

1. Basic Pattern Search

Find lines containing the keyword Cantech:

egrep 'Cantech' cantech.txt

2. Use the --color option to highlight the word Cantech in the output.

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

3. Case-Insensitive Search

egrep -i 'cantech' cantech.txt

The above command looks for the word cantech in the cantech.txt file and shows all matching results, regardless of case. It will match cantech, Cantech, or CANTECH.

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

The above command looks for the Hello keyword in every file inside the /home/cantech_user/dir1 directory, including all its subdirectories.

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 Command Usage

1. Grouping Patterns

Find lines where Example is followed by from:

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

The above command looks for lines in cantech.txt where any text appears after the Example keyword and before the from keyword.

2. Match Whole Words

egrep -w 'Cantech' cantech.txt

The above command looks for lines in cantech.txt that include cantech as a complete word, not as part of another term such as cantechs.

3. Exact Line Match

egrep -x 'Cantech' cantech.txt

The above command finds lines in cantech.txt that contain the Cantech keyword exactly, without any extra characters attached.

Working with Regular Expressions Using the egrep Command in Linux

Follow the steps below to apply regular expressions with the egrep command for filtering search results.

1. Make a new file named sample.txt.

nano sample.txt

2. Insert the content below into the sample.txt file.

error occurred at 10:45
warning: disk space is low
all systems operational
warning logs updated at 09:00
log entry recorded
this is just an error logs
error
success
network issues at 12:30

Save your changes and exit the file.

3. Anchors: Match Lines Starting with a Word (^)

egrep '^error' sample.txt

4. Match Lines Ending with a Word ($)

egrep 'success$' sample.tx

5. Character Classes: 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

6. 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 servers. 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.

FAQs

What is egrep?

egrep is a Linux command used to search for specific words or patterns in files. It works like grep but supports more advanced search options.

What is egrep command in Linux?

The egrep command in Linux helps you find matching text or patterns inside files. It’s useful when you need to search through large files quickly.

What is the difference between grep and egrep?

grep and egrep are similar, but egrep allow extra pattern options. Simply, egrep is an upgraded version of grep with more flexibility.

October 8, 2025