What is egrep Linux command?
egrep
is a powerful Linux command used to search for specific patterns within files. It is an extension of the grep
command with support for extended regular expressions. egrep
offers advanced pattern matching capabilities, making it a versatile tool for users looking to find specific text within files quickly and efficiently.
egrep Syntax:
egrep Options:
Option | Description |
---|---|
-i | Ignore case sensitivity |
-v | Invert the match (exclude pattern) |
-w | Match whole words |
-E | Interpret pattern as an extended regular expression |
-F | Interpret pattern as fixed strings (no regex) |
-c | Count the number of matching lines |
-o | Print only the matching part of the lines |
-n | Show line numbers along with the output |
-f file | Read patterns from a file |
-r | Recursively search subdirectories |
Parameters:
Parameter | Description |
---|---|
pattern | The pattern to search for in the file |
file | The file(s) to search the pattern in |
egrep Command Usage Examples:
Search for a Specific Word in a File
Searches for the word “word” in the file named “file.txt” and displays the matching lines.
Search for Lines Starting with a Specific Pattern
Displays lines from the file “file.txt” that start with the specified pattern.
Search for Lines Ending with a Specific Pattern
Displays lines from the file “file.txt” that end with the specified pattern.
Search for Lines Containing Either of Two Patterns
Displays lines from the file “file.txt” that contain either “pattern1” or “pattern2”.
Search for Lines Matching a Regular Expression
Displays lines from the file “file.txt” where the entire line consists of one or more digits.
{Questions}
Applications of the egrep command
- Searching for a specific pattern in files
- Filtering output based on regular expressions
- Extracting specific information from text
- Validating input based on patterns
- Comparing text against a pattern