3 UNIX / Linux egrep Command Examples

What is egrep?
3 egrep examples
Syntax and Options
Related Commands

What is egrep?

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

3 egrep Examples

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 ( )

Syntax and Options

Syntax:

egrep [options] [regexp] [files]

The options of egrep are same as grep. Few of them are shown below.

Short Option Long Option Option Description
-c –count Suppress normal output; instead print a count of matching lines for each input file. With the -v, –invert-match option (see below), count non-matching lines.
-L –files-without-match Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning will stop on the first match.
-l –files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.
-m –max-count Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines.
-o –only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.


Related Commands

grep
fgrep
rgrep

Comments on this entry are closed.

  • Joe July 31, 2011, 4:07 am

    Thank You

  • kalyan de May 8, 2012, 5:20 am

    Thanks ! Really helpful

  • manju May 28, 2012, 8:16 am

    good very useful..keep ur service..

  • Deb March 31, 2013, 11:11 pm

    Useful indeed. Thank you 🙂

  • sudheer October 7, 2013, 9:10 am

    Thanks a lot…

  • Cathy Webster January 22, 2014, 10:40 pm

    Great job. Very clear

  • radhika January 30, 2014, 3:23 am

    nice code….
    good job……………

  • Linus February 7, 2014, 6:13 pm

    Clear and consice.
    Muchas gracias!

  • sudhakar February 12, 2014, 1:25 am

    where is example to “fgrep”