egrep Command in Linux
Build an AI-First Career, Master the Complete Skillset
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
AI Forward Deployed Engineer Program
Full-stack engineering, production AI and client-facing consulting
+1000 moreOverview
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.
Options in egrep Command in Linux
-
-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.
-
-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'.
-
-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.
How Scaler Transformed Careers in Different Fields
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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
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.