Uncategorized

Difference Between Egrep vs Grep

Egrep vs Grep
Egrep vs grep

Grep is an amazing command line utility, it stands for “Global Regular Expression Print”. Grep command process text line by line and print any line which matches a specified pattern. Grep Command can be used to search for any kind of words, sentence and pattern in a text file or number of text file. Egrep stands for “Extended Global Regular Expressions Print”. It is a program which also scans a specified file line by line, returning back lines that contain a pattern matching a given regular expression. Grep and Egrep does the same thing more or less. The way they interpret the pattern is the only difference. Egrep interpret Pattern as an extended regular expression. Let’s dive into the egrep vs grep difference lectern.

egrep vs grep

Grep

Unlike egrep in grep, ?, +, {, |, (, and ), are rather treated as pattern instead of meta characters. By putting ‘backslash’ in front of meta character can let the grep to treat it as meta characters like \?, \+, \{, \|, \(, and \).

ls | grep '.txt|.py'

Now just consider the above example, in this command we are listing down all the files in the present working directory. Using pipe we are giving the output of ls command to grep. The grep command will check if there is any file with .text|.py extension.

Egrep

Now let’s see egrep side of the things. Egrep is efficient and fast when it comes to searching for a regular expression pattern in large text files as it treats meta-characters as is and doesn’t substitute them as strings like in grep, and hence you are freed from the burden of escaping them exclusively as in grep.

ls | egrep '.txt|.py'

Now consider again the same example with egrep, here egrep will look for files with either .txt or .py 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 but that’s an overhead for a power user since Linux command-line is all about reducing the amount of work to be done.

When we talk about egrep vs grep in terms of processing large chunks of text data, egrep is more efficient then grep.

Also check our Linux Commands cheat sheet

Conclusion egrep vs grep

Pointers given above are the differences between ‘grep’ vs ‘egrep’. Apart from difference, in the set of regular expressions used, and speed of execution, rest of command line arguments and options are same for both grep and egrep, even instead of “egrep” , “grep -E” is recommended to be used.

About the author

Ajay Verma

A Computer Science Graduate, who works extensively on open source projects. His Areas Of interest are: Network Security, Linux Administration, FOSS, Python, and C programming.

Add Comment

Click here to post a comment