Skip to content

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.

Terminal window
egrep [options] pattern file
OptionDescription
-iIgnore case sensitivity
-vInvert the match (exclude pattern)
-wMatch whole words
-EInterpret pattern as an extended regular expression
-FInterpret pattern as fixed strings (no regex)
-cCount the number of matching lines
-oPrint only the matching part of the lines
-nShow line numbers along with the output
-f fileRead patterns from a file
-rRecursively search subdirectories
ParameterDescription
patternThe pattern to search for in the file
fileThe file(s) to search the pattern in
Terminal window
egrep 'word' file.txt

Searches for the word “word” in the file named “file.txt” and displays the matching lines.

Search for Lines Starting with a Specific Pattern

Section titled “Search for Lines Starting with a Specific Pattern”
Terminal window
egrep '^pattern' file.txt

Displays lines from the file “file.txt” that start with the specified pattern.

Search for Lines Ending with a Specific Pattern

Section titled “Search for Lines Ending with a Specific Pattern”
Terminal window
egrep 'pattern$' file.txt

Displays lines from the file “file.txt” that end with the specified pattern.

Search for Lines Containing Either of Two Patterns

Section titled “Search for Lines Containing Either of Two Patterns”
Terminal window
egrep 'pattern1|pattern2' file.txt

Displays lines from the file “file.txt” that contain either “pattern1” or “pattern2”.

Search for Lines Matching a Regular Expression

Section titled “Search for Lines Matching a Regular Expression”
Terminal window
egrep '^[0-9]+$' file.txt

Displays lines from the file “file.txt” where the entire line consists of one or more digits.

{Questions}

  1. Searching for a specific pattern in files
  2. Filtering output based on regular expressions
  3. Extracting specific information from text
  4. Validating input based on patterns
  5. Comparing text against a pattern