Difference Between grep and egrep

The main difference between grep and egrep is that grep is a command that allows searching content according to the given regular expression and displaying the matching lines while egrep is a variant of grep that helps to search content by applying extended regular expressions to display the machining lines.

The operating system is the core of the entire computer system. It works as the interface between the user and the hardware. Furthermore, it performs memory management, process handling, task scheduling and many more. UNIX is a stable operating system. Linux is a free and open source operating system that is based on UNIX. These operating systems provide the opportunity for the users to enter commands to the Command Line Interface (CLI) to accomplish tasks. There is a large number of commands with options. Two of them are grep and egrep. The grep allows to search patterns using regular expressions whereas egrep allows using extended regular expressions. Overall, egrep allows searching multiple patterns at a time using a single command easily.

Key Areas Covered

1. What is grep
     – Definition, Examples
2. What is egrep
     – Definition, Examples
3. Relationship Between grep and egrep
     – Outline of Association
4. Difference Between grep and egrep
     – Comparison of Key Differences

Key Terms

grep, egrep, Operating System

Difference Between grep and egrep - Comparison Summary

What is grep

The grep command allows the users to scan the documents and to display the result according to the given regular expression.

Difference Between grep and egrep

Figure 1: Command Line Interface

grep “test” example1.txt

This command displays all the lines that start with the word test, in the example1.txt file.

grep “test” example1.txt example2.txt

This command will search the word test in both files and display the result.

grep  “test”  example1.*

This will find the example1 files with any format.

Following command display 2 lines after the given string “test”.

grep  -A 2  “test” file1.txt

Similarly, the following command displays 3 lines before the given string “test”.

grep  – B 3 “test” file1.txt

Those are few examples of grep command.  

Overall, grep command allows searching the given regular expression and displaying the matching lines.

What is egrep

There are various grep implementations available in many operating systems. egrep is one of them. This variant uses extended regular expressions. 

egrep “ab | bc” test1.txt.

This command will search the patterns ab, bc in the file test1.txt.

egrep  ‘^[a-z  A-Z] +$’  test1.txt.

This command will search any line in test1.txt which starts with an alphabetic word which also ends the line.

egrep –c  “ ^ ab | bc $ ”  test1.txt.

This command will count the lines in test1.txt file that start with ab or ends with bc.

Those are few examples of egrep.

Relationship Between grep and egrep

  • grep –E is the same as egrep.

Difference Between grep and egrep

Definition

The grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. The egrep is a variation of grep that is available in operating systems to perform searching using extended regular expressions.

Representation

Grep represents Global Regular Expressions Print whereas egrep represents Extended Global Regular Expressions Print.

Usage

The grep uses regular expressions for searching. The egrep uses extended regular expressions for searching. 

Meta Characters

Grep consider +,? , |  etc. as patterns. It is required to use them with a backslash to treat them as meta characters. e.g – \? , \+, \{, \|.  The egrep consider +,?, | etc. as meta characters.

Conclusion

The difference between grep and egrep is that the grep is a command that allows searching content according to the given regular expression and displaying the matching lines while egrep is a variant of grep that allows to search content by applying extended regular expressions to display the machining lines. In brief, grep and egrep performs the same functionality, but the method they interpret the pattern is different.

Reference:

1. “Linux Egrep Command Help and Examples.” Computer Hope, 29 Dec. 2017, Available here.

Image Courtesy:

1. “Grep-tutorial-05” By Баатарцогт Дашзэвэг – Own work (CC BY-SA 4.0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply