The egrep command in Linux is a variant of the grep command. It is very useful for searching patterns inside files and directories. The egrep provides the same functionality as the grep -E, which interprets patterns as regular expressions. In this article, I will describe the useful applications of the egrep command in Linux with hands-on examples.
A. Description
The egrep command in Linux is the extended version of the grep command. The egrep understands the extended regular expressions that describe a set of strings constructed with various arithmetic characters and operators.
B. Syntax
The syntax for the egrep command is pretty simple. It contains the command itself followed by some specific options and the pattern you want to grep.
grep [OPTION]... PATTERNS [FILE…]
C. Options
Many useful options are available for the egrep command. I have listed some of the most used ones here. However, to learn more about the egrep command you can always check the man page.
man egrep
Useful Options
- -A=NUM: Prints specified number of lines after the matched pattern.
- -B=NUM: Prints specified number of lines before the matched pattern.
- -C=NUM: Prints specified number of lines before and after the matched pattern.
- -c/–count: Counts the number of matching lines.
- -R: Recursively reads files within each directory. Follows all symbolic links.
- -r: Recursively reads files within each directory.
- -l: Suppresses output by printing only file names
- -i: Ignores cases while matching patterns
- -w: Selects only whole words as matched patterns
Practical Examples of the “egrep” Command in Linux
The egrep command in Linux is a useful command that enables users to search for words or characters in matching lines/files. Some of the most useful applications of the egrep command have been illustrated below.
Example 1: Searching Inside a File Using the “egrep” Command in Linux
You can search for a string inside a specific file using the egrep command in Linux. In this example, I will search to match the string “page” inside the file “text.txt”. You can do the same by following the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep page text.txt
➌ Now, press the ENTER button.
Output >
In the given image, you can see that I have found the desired string inside the “text.txt” file.
Similar Readings
- The “grep” Command in Linux [10+ Practical Examples]
- The “wc” Command in Linux [15 Practical Examples]
- The “sort” Command in Linux [16 Practical Examples]
Example 2: Searching Inside Multiple Files Using the “egrep” Command in Linux
You can search inside multiple files for a certain string using the egrep command in Linux. In this example, I will search the string “page” inside the files “text.txt” and “text2.txt”. To do the same you can follow the steps below.
Steps to Follow >
➊ At first go to the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep page text.txt text2.txt
➌ Now, hit the ENTER button.
Output >
In the image below, you can see that I have found the desired string inside the specified files.
Example 3: Using Regular Expressions With the “egrep” Command in Linux
You can use regular expressions to search for a pattern in a file using the egrep command in Linux. The regular expressions include the characters *, ?, _ to match certain patterns. In this example, I will search for the pattern “pa_*” inside the file “text.txt”. You can do the same by following the given process.
Steps to Follow >
➊ At first launch the Ubuntu Terminal.
➋ Write the following command in the command prompt:
egrep pa_* text.txt
➌ Press the ENTER button.
Output >
In the image below, you can see that I have found the desired pattern inside the “text.txt” file.
Note: The egrep command in Linux allows you to use a number of wildcard characters within the extended regular expressions. Some of the wildcard characters and its application are listed below.
- The asterisk (*): Indicates any number of characters including zero characters.
- The question mark (?): Indicates any single character.
- The underscore (_): Indicates any single character.Same as the question mark (?).
- The vertical bar (|): Matches characters written on either or both sides of the bar (|).
Similar Readings
- The “nano” Command in Linux [13 Practical Examples]
- The “cut” Command in Linux [8 Practical Examples]
- The “jed” Command in Linux [8 Practical Examples]
Example 4: Finding Specific Characters Using the “egrep” Command in Linux
You can search for specific characters inside a file using the egrep command in Linux. There are several ways to achieve this task. You can mention the characters explicitly or provide a range of characters. To find characters with this command you can use logical operators as well. Below I have described the multiple ways of finding specific characters.
Case A: Finding Characters Within a Range Using the “egrep” Command in Linux
To find characters within a range follow the steps below.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep [b-d] text2.txt
➌ Hit the ENTER button.
Output >
In the image given, you can see that I have found the characters b, c, and d presented in the file “text2.txt” using the range [b-d].
Case B: Finding Specified Characters Using the “egrep” Command in Linux
To find specified characters inside a file follow the given steps.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep [bcd] text2.txt
➌ Press the ENTER button.
Output >
In the following image, you can see that I have found the characters b, c, and d presented in the file “text2.txt” by specifying the characters within square brackets.
Case C: Finding Specified Characters With Logical Operator Using the “egrep” Command in Linux
To pipe specific characters within a range follow the steps below.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep b|c|d text2.txt
➌ Hit the ENTER button.
Output >
In the image below, you can see that I have found the characters b, c, and d presented in the file “text2.txt” using the logical operator OR (|).
Example 5: Finding Specific Word Using the “egrep” Command in Linux
You can search for a specific word inside a file using the egrep command in Linux with option -w. In this example, I will search the word “pages” inside the file “text2.txt”. You can do the same by following the steps below.
Steps to Follow >
➊ Launch the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep -w pages text2.txt
➌ Press ENTER.
Output >
In the given image, you can see that I have found the desired word inside the “text2.txt” file.
Example 6: Searching for a Pattern in a Directory/Subdirectory Using the “egrep” Command
You can look for a pattern within a whole directory or within the associated subdirectories using the egrep command in Linux. The command along with option -r allows the system to look inside the subdirectories recursively. You will find the individual applications of the command below.
Case A: Searching for a Pattern Inside Current Directory Using the “egrep” Command
To search for a pattern inside the files of the current directory follow the steps below.
Steps to Follow >
➊ Go to your Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep page *
➌ Now, press the ENTER button.
Output >
In the given image, you can see that I have found the desired pattern inside the current directory.
Case B: Searching for a Pattern Inside Subdirectories of Current Directory Using the “egrep” Command
To search for a pattern inside the files of the subdirectories follow the steps below.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep -r page *
➌ Now, press the ENTER button.
Output >
In the following image, you can see that I have found the desired pattern inside the files of the subdirectories.
Example 7: Displaying Extra Lines Before/After the Desired Pattern Using the “egrep” Command
You can print the line containing your desired pattern as well as a specified number of lines before and after the pattern. To accomplish this, you will need to use the options -B or, -A or, -C as well as the specified number of lines along with the egrep command in Linux. Go through the given examples to learn more about the applications of these options.
Case A: Displaying Lines Before the Desired Pattern Using the “egrep” Command
To display lines before the desired pattern follow the steps below.
Steps to Follow >
➊ Launch the Ubuntu Terminal.
➋ Write the following command in the command prompt:
egrep -B 2 pages text2.txt
➌ Now, hit the ENTER button.
Output >
In the following image, you can see that I have printed 2 lines before the line containing the match “pages”
Case B: Displaying Lines After the Desired Pattern Using the “egrep” Command
To display lines after the desired pattern follow the steps below.
Steps to Follow >
➊ Launch the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep -A 2 pages text2.txt
➌ Now, press the ENTER button.
Output >
In the following image, you can see that I have printed 2 lines after the line containing the match “pages”
Case C: Displaying Lines Before and After the Desired Pattern Using the “egrep” Command
To display lines before and after the desired pattern follow the given steps.
Steps to Follow >
➊ Open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep -C 2 pages text2.txt
➌ Now, press the ENTER button.
Output >
In the image below, you can see that I have printed 2 lines both before and after the line containing the matched pattern “pages”
Similar Readings
Example 8: Printing File Names Containing a Certain String Using the “egrep” Command in Linux
You can print only the file names that contain a specific pattern as it’s content using the egrep command in Linux. To do so you will need to combine the options -R and -l. In this example, I will print all the file names that have the pattern “page” inside their content. You can do the same by following the steps below.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep -R -l page *
➌ Now, press the ENTER button.
Output >
In the given image, you can see that I have listed all the desired files.
Example 9: Counting Lines Having Specified String Using the “egrep” Command
You can count the number of patterns repeated in a file using the egrep command with the -c option in Linux. In this example, I will count the number of the pattern “page” presented inside the file “text.txt”. To do the same you can follow the steps below.
Steps to Follow >
➊ At first go to the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep -c page text.txt
➌ Hit the ENTER button.
Output >
In the following image, you can see that I have counted the number of matched patterns inside the “text.txt” file.
Example 10: Ignoring Cases While Searching With the “egrep” Command in Linux
You can search for case-insensitive patterns using the egrep command in Linux with the option -i. In this example, I will search for the pattern “page” while ignoring its case inside the file “text2.txt”. To do the same you can follow the given process.
Steps to Follow >
➊ Launch the Ubuntu Terminal.
➋ Type the following command in the command prompt:
egrep -i page text2.txt
➌ Strike the ENTER button.
Output >
In the image below, you can see that I have found the desired pattern ignoring its case the “text2.txt” file.
Conclusion
In this article, I have illustrated the most common uses of the egrep command in Linux. Learning these practical examples will help you navigate through the files from the command line. I hope, the completion of the given tasks will help you with your experience in Linux and make you a power user.
Similar Readings
- The “paste” Command in Linux [6 Practical Examples]
- The “split” Command in Linux [6 Practical Examples]
- The “sed” Command in Linux [7 Practical Examples]
- The “tr” Command in Linux [6 Practical Examples]
- The “uniq” Command in Linux [6 Practical Examples]
- The “printf” Command in Linux [8 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners