Difference between grep and egrep

| | 2 min read

Grep is an amazing utility on command line. Every one might have heard of grep. It's a great tool to search for a pattern with in a directory or in a file. On searching for a pattern using grep, the default result will print the pattern along with the line number at which the pattern is found. Grep is one of those handy commands on bash that are often used.

Pre requisites for grep

Grep comes installed with an ubuntu machine, if it is not installed, it could be done using,

sudo apt-get grep install;

grep and egrep

I have played around with grep for a while, at some point I have noticed egrep. grep and egrep does the same function, but the way they interpret the pattern is the only difference.

Grep stands for "Global Regular Expressions Print", were as Egrep for "Extended Global Regular Expressions Print". The pattern often treated as a regular expression, for which e in egrep stands for "Extended Regular Expressions" abbreviated 'ERE' is enabled in egrep.

grep -E is same as egrep

In egrep, +, ?, |, (, and ), treated as meta characters. Where as in grep, they are rather treated as pattern instead of meta characters. By including 'backslash' followed by meta character can let the grep to treat it as meta characters like \?, \+, \{, \|, \(, and \).

ls | grep '.txt|.php'

Let us consider this example, in this we are listing all the files in the present working directory. Using pipe we are passing the output of ls to grep. The grep command will check whether there is any file with .text|.php extension.

ls | egrep '.txt|.php'

Now consider the same example with egrep, here egrep will check for files with either .txt or .php extension. By using egrep we can even search for multiple pattern, files at a time using one command. We can make grep also do the same by escaping the characters.