egrep Command in Linux

Back

The egrep command is more forgiving, it allows you to use some unusual characters in your search, including +,?,|,( and ) . While it's possible to set up grep to search for these characters with the help of the backslash, command can be awkward.


egrep is same as ‘grep -E’ or ‘grep –extended-regex’, which uses extended regular expression.

Syntax:
   egrep [options] [regexp] [files]

First create the following employee.txt sample file.
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

1. Search for Specific Characters
    The following example searches for either J, or N, or R.

$ egrep [JNR] employee.txt
200  Jason   Developer  Technology  $5,500
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

2. Search for a Range
    The following example searches the range 6-9. i.e It searches for 6, or 7, or 8, or 9.

$ egrep [6-9] employee.txt
300  Sanjay  Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000

3. egrep OR Example
    Pipe symbol is used for egrep OR. The following searches for either Marketing or DBA.

$ egrep 'Marketing|DBA' employee.txt
400  Nisha   Manager    Marketing   $9,500
500  Randy   DBA        Technology  $6,000
Note: egrep supports the extended grep characters: +, ?, |, and ( )