Friday, 24 January 2025

egrep (Extended grep) in Unix

 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:

  1. Multiple Patterns:

    egrep 'apple|orange|banana' file.txt
    

    Matches any line containing "apple", "orange", or "banana".

  2. Repetition:

    egrep 'a{2,}' file.txt
    

    Matches lines with "a" repeated 2 or more times.

  3. Optional Characters:

    egrep 'colou?r' file.txt
    

    Matches "color" or "colour".

  4. Find Lines with a Specific Range of Characters: egrep '[0-9]{3,5}' file.txt

          Matches lines containing a number with 3 to 5 digits (e.g., 123, 12345).
-------------------------------------------------------------------------------------------------------------

Find Lines with Repeated Patterns:
egrep '(ab)+c' file.txt

Matches lines containing "ab" repeated one or more times, followed by "c" (e.g., "abc", "abababc").

-------------------------------------------------------------------------------------------------------------

Match Words with Optional Characters:

egrep 'gr(e|a)y' file.txt

Matches "grey" or "gray".

-------------------------------------------------------------------------------------------------------------

Search for Lines with a Specific Word or Another (Logical OR):

egrep 'cat|dog' file.txt
or
grep -E 'cat|dog' file.txt

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.

Use egrep when complex pattern matching is needed!

No comments:

Post a Comment

how to create dml dynamically in Ab-initio

 $[ begin let int i = 0; let string(int) complete_list = "emp_nm,billable,age,designation,location"; let string(int) file_content ...