You use egrep (Extended grep) in Unix when you need to search for patterns using extended regular expressions (ERE). It supports additional features like +, ?, |, and {} directly without escaping.
Example Use Cases:
-
Multiple Patterns:
egrep 'apple|orange|banana' file.txtMatches any line containing "apple", "orange", or "banana".
-
Repetition:
egrep 'a{2,}' file.txtMatches lines with "a" repeated 2 or more times.
-
Optional Characters:
egrep 'colou?r' file.txtMatches "color" or "colour".
Find Lines with a Specific Range of Characters: egrep '[0-9]{3,5}' file.txt
Matches lines containing "ab" repeated one or more times, followed by "c" (e.g., "abc", "abababc").
-------------------------------------------------------------------------------------------------------------
Match Words with Optional Characters:
Matches "grey" or "gray".
-------------------------------------------------------------------------------------------------------------
Search for Lines with a Specific Word or Another (Logical OR):
Matches lines containing either "cat" or "dog".
Why Use egrep?
egrep is simply a shorthand for grep -E, so using it is more convenient for extended regular expressions. However, newer systems recommend grep -E because egrep is considered deprecated in some environments.
egrep when complex pattern matching is needed!
No comments:
Post a Comment