Linux egrep Command With Examples

September 8, 2022

Introduction

The egrep (Extended Global Regular Expression Print) command is a text processing tool that searches for patterns or regular expressions in a specified location. The tool provides the same output as grep -E, but works faster.

This guide explains how to use the Linux egrep command through practical examples.

Linux-egrep command with examples

Prerequisites

  • Access to the terminal.
  • text file to work on. This guide uses the file my_text as an example.

Linux egrep Syntax

The basic egrep syntax looks like this:

egrep [options] [search_term] [location]

The egrep command cannot run without at least one search term and a search location. Parts of the egrep syntax are:

  • Options. Arguments that modify and specify the search process.
  • One or multiple search terms. A string, multiple strings, or a regular expression.
  • Location. A file, directory, or path to a file in which to conduct the search.

egrep Linux Options

Using different options with egrep specifies the process. Common egrep command options are shown in the table below:

OptionDescription
-cCounts matching lines.
-vActivates invert matching.
-lDisplays only file names with matches.
-LDisplays file names only, without matches.
-eAllows using a hyphen at the beginning of the pattern.
-HDisplays file names in addition to the standard output.
-iActivates case-insensitive search.
-oOutputs only the matched string and not the entire line.
-wOutputs only lines that contain the whole words.
-xShows only strings matching the entire line.
-nShows line numbers along with the matched line.
-rSearches for the search term in the specified directory and its subdirectories.
-bShows the position of the matches in the file
-B (n)Prints the line with the search string and n previous lines.
-A (n)Prints the line containing the search string and n lines after.
-C (n)Prints the line containing the search string and n lines before and after.

egrep Examples

An extension of grep, egrep is a useful tool for finding regular expressions as well as multiple patterns with a single command. The egrep command also uses symbols like ?, +, or | as meta characters by default, eliminating the need to use the backslash.

The diversity allows for multiple practical uses of egrep, and common uses are explained in the following examples.

Important: The egrep search feature is case sensitive, meaning uppercase and lowercase letters are interpreted differently.

Search for a String in a File

The most common use of egrep is searching for a string in a file. For example, find the string man in the my_text file with:

egrep man my_text
egrep man my text terminal output

The command prints every line containing the search string. To print only the string and not the entire line, use the -o flag:

egrep -o man my_text
egrep o terminal output

Combine -o with the -b flag to find where matches are located in the file:

egrep -b -o man my_text
egrep o b terminal output

The -b flag also works without other arguments, printing the entire line and the location of the string:

egrep -b man my_text
egrep b terminal output

Find Regular Expressions

The egrep command works with extended regular expressions, which contain more characters for specifying the matching pattern (*,+,? |). The metacharacters are called quantifiers and specify the number of appearances.

The asterisk (*), for instance, matches zero or more appearances.

egrep 'a*n' my_text
egrep regular expressions terminal output

The output prints all lines containing the character n. However, the character a appears:

  • Zero times - like in reference.
  • One time - like in strings man or an.

Note: Learn how grep Regex works to start using regular expressions.

Find Specific Characters

Find different characters in a text with egrep and bracket expressions. Bracket expressions allow egrep to match different characters.

For example, find capitalized A, B, or C letters in the file my_text:

egrep [ABC] my_text
egrep ABC my-text terminal output

The command highlights individual letters and prints lines containing them.

However, using caret ^ at the beginning of a bracket expression searches for everything except for the specified string. For instance, find anything except for capital A, B, or C with:

egrep [^ABC] my_text
egrep caret and bracket expressions terminal output

The command highlights everything in printed lines except capitalized A, B, and C letters.

Pipe Characters

Another way to search for A, B, or C is to pipe the characters. Since egrep recognizes | as a metacharacter, execute:

egrep 'A|B|C' my_text
egrep piping characters terminal output

Note: Make sure to encase the search string in single quotes when piping.

Piping works with longer strings as well. To search for two different expressions and not characters, use pipe with egrep:

egrep 'system|page' my_text
egrep piping expressions terminal output

The command prints all lines containing strings system or page.

Search for a Range

A hyphen within a bracket expression creates a range expression. Executing egrep with a numbered range as the search string outputs all lines with specified numbers. For instance, find numbers 2, 3, 4, or 5 with:

egrep [2-5] my_text
egrep range 2-5 terminal output

The egrep command works with letter ranges as well. To find characters a, b, c, and d, execute:

egrep [a-d] my_text
egrep range letters terminal output

Bracket expressions allow multiple ranges. To search for a, A, b, B, c, C, d and D characters, run:

egrep [a-dA-D] my_text
egrep range lowercase and uppercase letters terminal output

The egrep command supports standard classes, encased in double brackets, simplifying bracket expressions further. For example, search all uppercase letters with:

egrep [[:upper:]] my_text
egrep standard character classes terminal output

Print Only Number of Matched Lines

Use the -c flag to display the total number of lines containing the search term:

egrep -c man my_text
egrep c terminal output

The total line count remains the same even if the term appears more than once per line.

Display Lines Before or After the Search String

To print the line containing the search string as well as a specific number of lines before and/or after it, use egrep with a -B (Before), -A (After), or -C (Containing) flag.

For instance, print the line containing the search string Library and two lines before it with:

egrep -B 2 Library my_text
egrep B 2 terminal output

Print two lines after the search term with the -A flag:

egrep -A 2 Library my_text
egrep A 2 terminal output

Show the match line and two lines before and after with the -C flag:

egrep -C 2 Library my_text
egrep C 2 terminal output

Search for the String in a Directory

To find a string in any file in the current directory, use an asterisk (*) as the location:

egrep man *
egrep man asterisk terminal output

The command prints all occurrences of man in the current directory but not its subdirectories. To access the subdirectories as well, add the -r flag to egrep:

egrep -r man *
egrep r terminal output

The output prints all the lines and file names that contain the string man from all the files in the current directory and its subdirectories.

Find Complete Words Only

The egrep command prints any line containing the string, even when being a part of a larger string. For example, when man is the search term, egrep displays lines containing man in manual as well:

egrep substrings terminal output

To search for the term man as a full word only, use the -w flag with egrep:

egrep -w man my_text
egrep only whole words w terminal output

The output shows that egrep -w finds man as an independent string only. The command ignores man when included in longer strings, like manual.

Print File Names

The egrep command doesn't print file names of the matches by default. Use -H with egrep to display file names:

egrep -H man *
egrep H terminal output

To omit the line containing the search pattern and to print the file name only, use the -l flag:

egrep -R -l man *
egrep R l terminal output

The output displays five files in the current directory and its subdirectory containing the pattern man.

Ignore Case

The egrep command is case sensitive, meaning uppercase and lowercase letters are interpreted differently. For example, when the is the search string, egrep only returns lowercase matches and ignores uppercase ones:

egrep case-sensitive search terminal output

Use the -i option to disable case-sensitive search:

egrep -i the my_text
egrep case insensitive search terminal output

Perform Inverted Search

Use the -v flag to print strings that do not match the search pattern. For instance, omit every line with the word man (regardless of the case) in the output with:

 egrep -v -i man my_text
egrep v terminal output

To perform even more advanced output filtering, combine -v with other arguments. For instance:

egrep -v -i [a-d] my_text
egrep v range terminal output

The arguments instruct egrep to return lines that do not contain letters from a to d, ignoring the case. The output prints only one line containing the string SYNOPSIS.

Conclusion

Going through the examples in this guide, you learned how to use egrep to find patterns or extended regular expressions in a file. Next, learn how to compare two files with the Linux comm command.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Rename Files in Linux
November 20, 2023

There are several ways to rename files in Linux, including using the GUI and terminal commands. This tutorial shows you how to rename different files in Linux using the mv and rename commands...
Read more
Linux sed Command: How To Use the Stream Editor
April 6, 2022

SED is a text stream editor for modifying files in a fast and efficient way. Learn how to use the sed command and its options through easy-to-follow examples...
Read more
How to Use the locate Command in Linux
November 7, 2024

The locate command is a utility that allows users to quickly search for files or directories in Linux. This tutorial shows how to install and effectively use the locate command.
Read more
AWK Command in Linux with Examples
October 28, 2021

This tutorial shows how to use the awk command in Linux to perform advanced text manipulation and processing. Learn to format a complex..
Read more