egrep command in Linux with examples
Last Updated :
02 Sep, 2024
The ‘egrep’ command in Linux is a powerful pattern-searching utility that belongs to the family of grep functions. It functions similarly to ‘grep -E’ by treating patterns as extended regular expressions, allowing more complex pattern matching without the need to escape special characters. This command is particularly useful for quickly searching through files and directories for specific text patterns.
What is the ‘egrep’ Command?
‘egrep’ stands for “extended grep” and is a part of the grep family of commands used for text processing and pattern matching in Linux. It is preferred over basic grep when working with extended regular expressions, as it inherently supports meta-characters without requiring them to be escaped, thus simplifying complex searches. The ‘egrep‘ command is also known for its speed and efficiency, making it a popular choice among system administrators and developers.
Syntax:
egrep [ options ] 'PATTERN' files
where,
- ‘[options]’: Flags that modify the command’s behavior.
- ‘PATTERN’: The regular expression pattern to search for.
- ‘[files]’: The files or directories where the search will be performed.
Example:
How ‘egrep’ differs from ‘grep’
While both ‘egrep’ and ‘grep’ are used for pattern searching, the key difference lies in their handling of regular expressions. ‘egrep’ supports extended regular expressions by default, allowing the use of complex patterns without needing to escape special characters. This capability makes ‘egrep‘ faster and more efficient in scenarios where advanced pattern matching is required.
Common Options with ‘egrep’ Command
Here are some of the most commonly used options for ‘egrep’, along with examples to illustrate their usage:
1. ‘-c’ (Count Matches)
Used to counts and prints the number of lines that matched the pattern and not the lines.
2. ‘-v’ (Invert Match)
It prints the lines that does not match with the pattern.
3. ‘-i’ (Ignore Case)
Ignore the case of the pattern while matching.
4. ‘-l’ (List Matching Files)
Prints only the names of the files that matched. It does not mention the matching line numbers or any other information.
5. ‘-L’ (List Non-Matching Files)
Prints only the names of the files that did not have the pattern. Opposite of -l flag.
6. ‘-e’ (Pattern List)
Allows to use a ‘-‘ sign in the beginning of the pattern. If not mentioned the shell tries to execute the pattern as an option and returns an error.
7. ‘-w’ (Match Whole Words)
Prints only those lines that contain the whole words. Word-constituent characters are letters, digits and underscore. The matching substring must be separated by non-word constituent characters.
8. ‘-x’ (Match Entire Lines)
Prints only those lines that matches an entire line of the file.
9. ‘-m NUMBER’ (Limit Matches)
Continue to search for matches till the count reaches NUMBER mentioned as argument.
10. ‘-o’ (Print Only Matching Parts)
Prints only the matched parts of the line and not the entire line for each match.
11. ‘-n’ (Line Numbers)
Prints each matched line along with the respective line numbers. For multiple files, prints the file names along with line numbers.
12. ‘-r’ (Recursive Search)
Recursively search for the pattern in all the files of the directory. The last argument is the directory to check. ‘.’ (dot) represents the current directory.
Conclusion
The ‘egrep’ command is a powerful tool in the Linux command, offering advanced pattern matching capabilities that simplify searching through text files. With its ability to handle extended regular expressions efficiently and a wide range of options, ‘egrep’ can significantly enhance productivity when working with large datasets or complex text searches.