egrep
Search files for extended regular expression patterns
TLDR
Search for a pattern within a file
Search for a pattern within multiple files
Search stdin for a pattern
Print file name and line number for each match
Search for a pattern in all files recursively in a directory, ignoring binary files
Search for lines that do not match a pattern
SYNOPSIS
egrep [OPTIONS] PATTERN [FILE...]
Note: egrep is functionally identical to grep -E.
PARAMETERS
-i
Ignore case distinctions in both the PATTERN and input data.
-v
Invert the sense of matching, to select non-matching lines.
-c
Suppress normal output; instead print a count of matching lines for each input file.
-l
Suppress normal output; instead print the name of each input file from which output would normally have been printed.
-n
Prefix each line of output with the 1-based line number within its input file.
-r, -R
Recursively search subdirectories listed.
-w
Select only those lines containing matches that form whole words.
-x
Select only those matches that match the whole line.
-f FILE
Obtain PATTERNs from FILE, one per line.
-o
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
--color[=WHEN]
Highlight matches. WHEN can be 'always', 'never', or 'auto'.
DESCRIPTION
egrep is a command-line utility used for searching plain-text data for lines that match a regular expression. It stands for "Extended GreP" and is functionally equivalent to the `grep -E` command. Its primary distinction from basic `grep` is its inherent support for Extended Regular Expressions (EREs), which offer more powerful pattern matching features like `+` (one or more occurrences), `?` (zero or one occurrence), `|` (alternation), and `()` (grouping). While still widely available, `egrep` is often implemented as a symlink or hardlink to `grep -E` in modern Linux distributions, making `grep -E` the preferred and more portable syntax for using EREs. It's an indispensable tool for system administrators and developers alike, enabling complex text pattern searches across files and standard input.
CAVEATS
egrep is considered deprecated in most modern grep implementations. It is strongly recommended to use grep -E instead, as egrep often exists merely as a symbolic link to grep -E for backward compatibility. This ensures better portability and consistency across systems. Differences in grep versions (e.g., GNU grep vs. BSD grep) can lead to slight variations in regular expression syntax or behavior, though EREs are largely standardized.
EXTENDED REGULAR EXPRESSIONS (ERES)
Extended Regular Expressions (EREs) offer more powerful and readable syntax compared to Basic Regular Expressions (BREs). Key ERE features supported by egrep (and grep -E) include:
- +: Matches one or more occurrences of the preceding character/group.
- ?: Matches zero or one occurrence of the preceding character/group.
- |: Acts as an OR operator, matching patterns on either side.
- (): Groups expressions, allowing operators to apply to the entire group or for backreferencing (though egrep typically doesn't support backreferences like grep does with BREs).
HISTORY
The original grep command was written by Ken Thompson in 1974. egrep (for Extended Regular Expressions) and fgrep (for Fixed-string Regular Expressions) were later added to the grep family to handle different types of pattern matching more efficiently. In older systems, these were distinct executables, often optimized for their specific tasks. Modern grep implementations, particularly GNU grep, have consolidated these functionalities. egrep and fgrep are now typically hard links or symbolic links to the grep executable, with the -E and -F options providing the same specialized behavior, respectively. This consolidation simplifies maintenance and ensures consistent behavior across the grep suite.