
The egrep (Extended Global Regular Expression Print) command in Linux is a powerful text searching tool for advanced pattern matching using extended regular expressions (ERE). egrep is part of the grep family of commands which include grep and fgrep. egrep supports extended regular expressions unlike grep making it more versatile for complex pattern matching.
This article explains how to use the egrep command in Linux to efficiently match patterns and perform file search tasks.
Regular expressions (regex) are sequences of characters that form search patterns. Regex searches specific keywords or strings using multiple characters and special symbols. egrep utilizes the following extended regular expression symbols to form search patterns with additional features:
^) or end ($) of a line. For example, ^error.[ ]). For example, [a-z].*), one or more (+), or zero or one (?) occurrences. For example, a*, a+, a?.|). For example: error|warning.( )). For example, (error|warning).\b). For example, \berror\b.^pattern$). For example, ^error$.BRE: grep (BRE) escapes certain metacharacters to use their special meaning, such as \+ or \?. For example, to search for one or more occurrences of the letter a in a file, use the + symbol to form the command grep 'a\+' file.txt.
ERE: egrep (ERE) metacharacters (+, ?, |) and parentheses () do not require escaping, making the syntax cleaner and more intuitive for complex patterns. For example, you can use the same grep character without escaping in egrep to form the command egrep 'a+' file.txt.
egrep Command SyntaxThe egrep command uses the following command syntax:
In the above command:
[options]: Include optional flags that modify the command's behavior.'pattern': Defines the pattern that you want to find.[file...]: Specifies the file you want to search.Follow the steps below to create example directories and files to test the egrep command.
Switch to your user's home directory.
Create the vultr.txt, Vultr.txt, and VULTR.txt files in your working directory.
Create two directories and a subdirectory.
The above command creates the dir directory and the sub_dir subdirectory under dir1. The -p option creates the parent directory if it does not exist.
Create a new sample vultr.txt file using a text editor like nano.
Add the following contents to the file.
Save and close the file.
egrep Command OptionsThe following list displays the most common egrep command options:
egrep Command in Linux with ExampleFollow the steps below to use the egrep command with practical examples.
Perform a basic pattern search for all lines that contain the Vultr keyword in the vultr.txt file.
Output:
Highlight the color of the Vultr keyword using the --color option.
Output:
Search for lines that contain the vultr keyword regardless of the case sensitivity.
The above command searches for the vultr keyword in vultr.txt and displays all results that match any case, ignoring case sensitivity. For example, vultr, Vultr, and VULTR match the search.
Output:
Run an inverted search to find the lines that do not contain the vultr keyword .
Output:
Count the matching lines containing the Vultr keyword.
Output:
Display lines that contain the Vultr keyword along with the respective line numbers.
Output:
Search multiple files to find lines that contain the Vultr keyword.
Output:
Recursively search for lines that contain the Hello keyword in a directory.
The above command searches for the Hello keyword in all files within the /home/vultr_user/dir1 directory and subdirectories.
Output:
Display the context before the Number keyword .
The above command displays two lines of context before each matching line that contains the Number keyword in vultr.txt along with the line numbers.
Output:
Display the context after the Number keyword .
The above command displays two lines of context after each matching line that contains the Number keyword in the vultr.txt file along with the line numbers.
Output:
Display the context around the Number keyword.
The above command displays two lines of context before and after each matching line that contains the Number keyword in vultr.txt file.
Output:
egrep Command OptionsFollow the steps below to use the advanced egrep command options.
Search for lines containing Example followed by the from keywords using group search.
The above command searches for lines in vultr.txt where any characters follow the Example keyword and then the from keyword .
Output:
Search for lines that contain the full vultr keyword.
The above command searches for lines in vultr.txt that contain vultr as a full term and not as part of another keyword like vultrs.
Output:
Search for lines that exactly match Vultr.
The above command searches for lines in vultr.txt that match the Vultr keyword without any additional characters.
Output:
egrep Command in LinuxFollow the steps below to use regular expressions with the egrep command to filter the search results.
Create a sample.txt file.
Add the following contents to the sample.txt file.
Save and close the file.
Anchors: Match the start ^ or end $ of a line.
Search for lines in a log file that start with the error keyword.
The above pattern matches all lines that begin with the error keyword. If a line starts with another term, that match fails.
Output:
Search for lines in a file that end with the success keyword.
The above pattern matches all lines that end with the success keyword. Any line with additional characters at the end fails the match.
Output:
Character Classes: Match any character in brackets [ ].
Search for any line that contains lowercase letters between a and f.
The above pattern matches any lowercase letter from a to f to return lines that contain at least any of the characters.
Output:
Search for lines with numeric values.
The above pattern matches any digit from 0 to 9 and displays all lines that contain numeric values.
Output:
Repetition: Match a keyword with zero or more *, one or more +, or zero or one ? occurrences.
Search for lines with zero or more occurrences of the a keyword.
Output:
Search for lines with one or more occurrences of the a keyword.
Output:
Search for lines that contain the logs keyword followed by an optional s (to match both log and logs) in the sample.txt file.
The above pattern matches all lines that contain log or logs. The (?) question mark symbol makes the s character optional.
Output:
Alternation: Match either pattern using the | pipe symbol.
Search for lines that contain either error or warning.
The above pattern matches all lines that contain either the error or warning keywords.
Output:
Grouping: Match group patterns using parenthesis ( ).
Search for lines that contain either the error or warning followed by the log keywords.
Output:
Word Boundaries: Match whole words only using \b.
Search for lines that include error as a complete keyword and not a part of another keyword such as errors.
Output:
Line Boundaries: Match whole lines only using ^pattern$.
Perform an exact search for lines that contain the error keyword without any additional text.
Output:
egrep with Other CommandsUse egrep with pipe to filter the output of another command as the egrep input. For example, run the following command to find lines that contain the data keyword in the ps aux command output.
Output:
Combine egrep with find to search for patterns in files listed by find. For example, run the following command to search for the Hello keyword in all .txt files in a directory.
The above find command searches all .txt files in the /home/vultr_user/dir1 directory and its subdirectories. Then, egrep searches for the Hello keyword in the files.
Output:
Combine egrep with xargs for a more efficient search. For example, search for the Hello keyword in all .txt files in a directory.
The above command finds all .txt files in the /home/vultr_user/dir1 directory and passes the output to egrep to search for the Hello keyword. xargs improves efficiency by passing multiple files to egrep in a single command.
Output:
Combine egrep with wc command.
The above command counts the number of times the Vultr keyword appears in the vultr.txt file. The -o option instructs egrep to print only the matching parts while wc -l counts the number of lines in the search results.
Output:
egrep Command in Linux with ExamplesSearch for lines that contain either the Greetings or the Vultr keywords from the vultr.txt file in a case-insensitive manner and display line numbers.
Output:
Count the number of lines that contain the vultr keyword and display the matching lines.
Output:
You have used the egrep command in Linux with practical examples, options, and syntax to search files for specific contents. egrep supports extended regular expressions making it suitable for a wide range of pattern-matching tasks. You can use egrep effectively for basic searches or complex text-processing tasks by integrating it with other Linux commands. For more information, run man egrep to view the command's manual page.
0 Comments
Be the first to comment and share your perspective with the community.