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.
- Access to the terminal.
- A text file to work on. This guide uses the file my_text as an example.
Linux egrep Syntax
egrep syntax looks like this:
egrep [options] [search_term] [location]
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:
|Counts matching lines.|
|Activates invert matching.|
|Displays only file names with matches.|
|Displays file names only, without matches.|
|Allows using a hyphen at the beginning of the pattern.|
|Displays file names in addition to the standard output.|
|Activates case-insensitive search.|
|Outputs only the matched string and not the entire line.|
|Outputs only lines that contain the whole words.|
|Shows only strings matching the entire line.|
|Shows line numbers along with the matched line.|
|Searches for the search term in the specified directory and its subdirectories.|
|Shows the position of the matches in the file|
|Prints the line with the search string and n previous lines.|
|Prints the line containing the search string and n lines after.|
|Prints the line containing the search string and n lines before and after.|
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
| 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.
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
The command prints every line containing the search string. To print only the string and not the entire line, use the
egrep -o man my_text
-o with the
-b flag to find where matches are located in the file:
egrep -b -o man my_text
-b flag also works without other arguments, printing the entire line and the location of the string:
egrep -b man my_text
Find Regular Expressions
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
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
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
The command highlights everything in printed lines except capitalized A, B, and C letters.
Another way to search for A, B, or C is to pipe the characters. Since
| as a metacharacter, execute:
egrep 'A|B|C' my_text
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 'system|page' my_text
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 command works with letter ranges as well. To find characters a, b, c, and d, execute:
egrep [a-d] my_text
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 command supports standard classes, encased in double brackets, simplifying bracket expressions further. For example, search all uppercase letters with:
egrep [[:upper:]] my_text
Print Only Number of Matched Lines
-c flag to display the total number of lines containing the search term:
egrep -c man my_text
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
-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
Print two lines after the search term with the
egrep -A 2 Library my_text
Show the match line and two lines before and after with the
egrep -C 2 Library my_text
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 *
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 -r man *
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
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:
To search for the term man as a full word only, use the
-w flag with
egrep -w man my_text
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
egrep command doesn't print file names of the matches by default. Use
-H with egrep to display file names:
egrep -H man *
To omit the line containing the search pattern and to print the file name only, use the
egrep -R -l man *
The output displays five files in the current directory and its subdirectory containing the pattern man.
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:
-i option to disable case-sensitive search:
egrep -i the my_text
Perform Inverted Search
-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
To perform even more advanced output filtering, combine
-v with other arguments. For instance:
egrep -v -i [a-d] my_text
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.
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.