- Unix Commands Reference
- Unix Commands - Home
egrep Command in Linux
The Extended Global Regular Expressions Print, also known as egrep, is a command-line text processing utility that searches for regex or patterns in a specific location and prints out the lines that match the pattern. The egrep command is a variant of the grep command and functions the same as grep -E, which supports extended regular expressions.
Table of Contents
Here is a comprehensive guide to the options available with the egrep command −
egrep Command in Linux
When we use the egrep command to search within a single file, the command outputs only the matching lines without showing the file name. However, if we search across multiple files, egrep will prepend each matching line with the file name to indicate where the match was found. Use the following syntax to search for a specified pattern within one or more files −
egrep [options] 'PATTERN' fileNames
We can use the egrep command with the options to modify the search behavior. The PATTERN represents a regex or searching pattern to match, and fileNames are the files being searched. For a profound understanding, we can check the egrep manual page using the following command −
man egrep
The manual page for egrep includes a description, synopsis, and valid options for egrep, as shown in the following screenshot −
Common Options with egrep Command
The table given below shows the available options that can be used to modify the searching behavior of the egrep command −
Option | Description |
---|---|
-c | This option counts and prints the number of matched lines rather than printing the matched lines. |
-v | It displays the unmatched lines. |
-i | It matches the pattern case-insensitively. |
-l | It displays only the names of the files that match, without including line numbers or additional details. |
-L | This is the inverse of the -l option as it shows only the names of files that do not contain the pattern. |
-e | It enables the use of a "-" sign at the beginning of the pattern, which prevents the shell from interpreting it as an option and returning an error. |
-w | It shows only those lines that contain the complete words, where words consist of letters, digits, and underscores. The matching substring must be separated by characters that are not part of words. |
-x | This option shows only those lines that match the complete line of the file. |
-m NUMBER | It limits the output to the first NUMBER matches of the pattern. The search stops once this number of matches is found. |
-o | It is used to print only the matched pattern/part of the line. |
-n | It shows the matched line as well as the respective line number. |
-r or -R | This option recursively searches for the specified pattern in all files of a specific directory. |
Examples of egrep Command in Linux
Let’s implement them practically to see how they work in Linux.
- Printing the Matched Lines
- Printing the Number of Matched Lines
- Printing Matched Lines Along with Line Numbers
- Printing Only Matched Part Instead of Complete Line
- Printing Only Exact Matches
- Printing the Names of Files That Contains Matched Pattern
- Searching for a Pattern Case-Insensitively
Printing the Matched Lines
We have a sample file "exampleFile.txt" whose content is shown in the following screenshot −
cat exampleFile.txt
We can run the egrep command without any option to print the matched lines in the specified file −
egrep cy exampleFile.txt
The output shows the lines of the "exampleFile.txt" file that contain the specified pattern "cy" −
Printing the Number of Matched Lines
Suppose we want to count and print the existence of a pattern "cy". For this purpose, we can execute the egrep command with the c option as follows −
egrep -c cy exampleFile.txt
The output shows that there are a total 4 lines in the "exampleFile.txt" that match the pattern "cy" −
Printing Matched Lines Along with Line Numbers
Let’s run the egrep command with the -n option to show the matched lines along with their respective line numbers −
egrep -n cy exampleFile.txt
The output shows the line numbers followed by the matched content −
Printing Only Matched Part Instead of Complete Line
In the following example, we use the egrep command with the -o option to print only the matched part of the line instead of the complete line −
egrep -o cy exampleFile.txt
For better understanding, we can use the -o and -n options combined to show the matched part along with the respective line number −
egrep -on cy exampleFile.txt
Printing Only Exact Matches
By using the -x option with egrep, the command will only print lines that match the entire line exactly to the pattern. This means the pattern must match the complete line, not just a substring within the line −
egrep -x cytune exampleFile.txt
Let’s use this option along with the -n option to print the line of the matched pattern as well −
egrep -xn cytune exampleFile.txt
Similarly, we can use the -w option to print the whole matched word −
egrep -w cytune exampleFile.txt
Printing the Names of Files That Contains Matched Pattern
In the following example, we use the -l option with the egrep command to print the file names that contain "cytune" −
egrep -l cytune sampleFile1.txt exampleFile.txt file00 exam
The output shows that among the specified files only two files (sampleFile1.txt and exampleFile.txt) contain the specified text "cytune" −
On the other hand, we can use the L option with the egrep command to print the inverse of the l option, i.e., the file names that don’t match the specified pattern −
egrep -L cytune sampleFile1.txt exampleFile.txt file00 exam
Searching for a Pattern Case-Insensitively
By default, the egrep command performs pattern-matching case sensitivity. However, we can use the -i option with the egrep command to ignore the case of a pattern while matching −
egrep CY exampleFile.txt egrep -i CY exampleFile.txt
Run these commands one by one to see the difference. When we execute the egrep without the -i option, it doesn't return anything, which means it doesn’t find any match in the exampleFile.txt. However, when we run the same command with the -i option, it shows the matched patterns −
Similarly, you can use any other valid option to modify the behavior of the egrep command according to your requirements.
Conclusion
The egrep command is a useful text processing tool in Linux that searches for patterns using extended regex. It functions similarly to grep -E and provides various options to modify search behavior, such as counting matches, displaying line numbers, and more.
The egrep command provides flexibility and precision to search within single or multiple files, making it a valuable command for text manipulation and pattern-matching tasks. In this article, we covered the basics of egrep, its syntax, and practical examples demonstrating how to use different options.