egrep Command in Linux

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

The egrep command in Linux is a powerful tool used for searching and manipulating text based on patterns. It stands for 'Extended Global Regular Expressions Print', and as the name implies, it extends the capabilities of the original grep command by supporting additional regular expression syntax. It is commonly used for searching through text files and filtering output from other commands.

Syntax of egrep Command in Linux

The basic syntax of the egrep command in Linux is as follows:

Where:

  • options: This is where you specify any flags or options that change how the egrep command behaves.
  • pattern: This is the regular expression that egrep will search for. It must be enclosed in quotes.
  • file: This is the file(s) in which egrep will search for the pattern. If no file is specified, egrep reads from the standard input.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Options in egrep Command in Linux

  1. -i: This option makes the egrep command in Linux case-insensitive.

    For example -

    Output:

    This command searches for the word 'linux' in 'example.txt', regardless of case.

  2. -v: This option inverts the search, meaning it will return lines that do not match the pattern.

    For example -

    Output:

    This command returns lines from 'example.txt' that do not contain the word 'error'.

  3. -r: This option makes the search recursive, meaning it will also search in subdirectories.

    For example -

    Output:

    This command searches for the word 'main' in the current directory and all its subdirectories.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example Usages

  • Search for a word in a file:

    Output:

    Explanation: This command searches for 'word' in 'example.txt' and prints the lines that contain it.

  • Count the number of lines that match a pattern:

    Output:

    Explanation: This command counts the number of lines in 'example.txt' that contain the word 'word'.

Tips

  • Use the -l option to print only the names of files that contain the pattern.

  • To search for multiple patterns, use the '|' character to separate them. For example: egrep 'pattern1|pattern2' file.txt

  • The '^' and '$' symbols can be used to match the start and end of a line, respectively.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Advanced Use Cases of egrep Command in Linux

  • Search for a pattern in the output of another command:

    Output:

    Explanation: This command lists all directories in the current location. The 'ls -l' command outputs a long listing of files and directories, and the egrep command filters out lines that don't start with 'd', which in 'ls -l' output indicates a directory.

  • Use grouping and quantifiers:

    Output:

    Explanation: This command searches for lines in 'example.txt' that contain the word 'error' or 'fail' repeated at least 2 times.

  • Match lines of a certain length:

    Output:

    Explanation: This command matches lines in 'example.txt' that are exactly 20 characters long. The '.' matches any character, and '{20}' specifies that the previous element must occur exactly 20 times.

Conclusion

  • The egrep command in Linux extends the functionality of the original grep command with additional regular expression syntax.

  • Common options for egrep include -i for case-insensitive search, -v to invert the search, and -r for recursive search.

  • Egrep can be used to search for patterns in the output of other commands, count lines that match a pattern, and much more.