Egrep is a command-line tool that searches text files for lines matching a pattern, using extended regular expressions. You type a pattern and a filename, and it prints every line that matches, making it a fast way to filter logs, configs, or any text. This article shows you the most useful egrep examples, from basic searches to advanced tricks.
What Is Egrep and Why Still Use It?
Egrep is the older name for grep -E on Linux and Unix systems. It interprets your pattern as an extended regular expression, meaning you get extra metacharacters like +, ?, |, and parentheses without needing to escape them. For example, in basic grep you write a\+ to match one or more ‘a’s; in egrep you write simply a+. That makes patterns shorter and easier to read.
Most modern systems still ship egrep as a separate binary, though the man page often says it is deprecated in favor of grep -E. The behavior is identical, so any example here works with either command. If your script uses egrep, it will continue to work on virtually every Linux distribution and macOS.
The main advantage of egrep over plain grep is the ability to combine alternatives and repetitions in a single pattern. Instead of running three greps and merging results, you write one pattern that matches all three cases. This is especially useful in log analysis, where you often look for several error codes or IP ranges at once.
Basic Egrep Syntax and Options
The general form is:
egrep [options] 'pattern' file1 file2 ...
If you give no file, egrep reads from standard input, which allows piping from other commands. Common options include:
-i– ignore case-v– invert match (show lines that do not match)-c– count matching lines instead of printing them-n– show line numbers-l– list only filenames that contain a match-w– match whole words only-o– print only the matched part, not the whole line-r– recursive search through directories
For example, to count how many times the word “error” appears in a log file, ignoring case:
egrep -ci 'error' app.log
The -c option counts lines, not occurrences. If a line has two errors, it counts once. To count total occurrences, use -o and pipe to wc -l:
egrep -oi 'error' app.log | wc -l
Using Alternation and Groups
The pipe symbol | means “or”. To match lines containing either “cat” or “dog”:
egrep 'cat|dog' pets.txt
Parentheses group parts of a pattern. To match “cat” or “dog” only when followed by “fish” or “food”:
egrep '(cat|dog)(fish|food)' menu.txt
This matches “catfish”, “dogfish”, “catfood”, and “dogfood”. Without parentheses, the pattern cat|dogfish|food would match “cat”, “dogfish”, or “food” independently, which is rarely what you want.
Groups also allow backreferences, but egrep (like grep -E) does not support backreferences reliably across all versions. For advanced backreference needs, use grep -P (Perl-compatible) instead.
Repetition: +, ?, and Curly Braces
Egrep adds three repetition operators that basic grep lacks:
+– one or more of the preceding character or group?– zero or one (optional){n,m}– between n and m repetitions
Examples:
egrep 'colou?r' file.txt
Matches both “color” and “colour” (the “u” is optional).
egrep '[0-9]+' file.txt
Matches lines with one or more digits. Without the plus, [0-9] would match lines with at least one digit, but the plus is redundant there. The plus becomes essential when you want to match a whole number: [0-9]+ matches “123”, not just “1”.
egrep 'a{2,3}' file.txt
Matches “aa” or “aaa”. This is useful for finding repeated characters or specific counts of a pattern, such as exactly 5-digit ZIP codes: [0-9]{5}.
Anchors and Character Classes
Anchors tie the pattern to the start or end of a line. The caret ^ matches the beginning, and the dollar sign $ matches the end.
egrep '^ERROR' log.txt
Finds lines that start with “ERROR”. To find lines that end with a question mark:
egrep '\?$' faq.txt
Note the backslash before the question mark, because a plain ? is a repetition operator. To match a literal ?, escape it.
Character classes let you match a set of characters. [aeiou] matches any vowel, [^0-9] matches any non-digit (the caret inside brackets negates). Ranges like [a-z] are handy for case-insensitive matching without the -i flag.
egrep '^[A-Z]' names.txt
Matches lines that start with a capital letter.
Practical Examples for Logs and Configs
Real-world usage often involves filtering logs or extracting data. Here are five common scenarios:
- Find all IP addresses in a file:
egrep -o '[0-9]{1,3}(\.[0-9]{1,3}){3}' access.log– this prints only the IP-like strings. Note that it may match invalid IPs like 999.999.999.999, but it is a good starting point. - Show lines with either “FATAL” or “ERROR” and a timestamp:
egrep '^[0-9]{4}-[0-9]{2}-[0-9]{2}.*(FATAL|ERROR)' app.log– matches lines beginning with a date like 2025-03-14. - Exclude debug lines:
egrep -v 'DEBUG' app.log– prints everything except debug messages. - Find lines with exactly 3-letter words:
egrep '\b[A-Za-z]{3}\b' file.txt– the\bis a word boundary (works in GNU egrep). - List all .conf files that mention “timeout”:
egrep -l 'timeout' /etc/*.conf– prints filenames only.
Common Mistakes and Pitfalls
One frequent error is forgetting to quote the pattern. If you type egrep cat|dog file.txt, the shell interprets the pipe as a command separator, so egrep runs with pattern “cat” and then the shell tries to run a command named “dog”. Always put the pattern in single quotes.
Another mistake is using + or ? without realizing they are extended operators. In basic grep, those characters are literal, so grep 'a+' searches for the string “a+”, not one or more a’s. That is exactly why egrep exists – to avoid escaping those characters.
Escaping inside character classes can be confusing. Inside brackets, most metacharacters lose their special meaning. For example, [.] matches a literal dot, not any character. To match a literal backslash, you need \\ in the pattern (four backslashes in the shell).
Performance can also be an issue. Egrep reads line by line, so on huge files it is still fast, but if you use a pattern with many alternations, the engine may slow down. For very complex regexes, consider grep -P or a tool like awk.
Egrep vs. Grep vs. Fgrep
Grep, egrep, and fgrep are three variants that differ in how they interpret the pattern:
- grep – basic regular expressions (BRE). Metacharacters like
+,?,|, and parentheses must be escaped to get their special meaning. - egrep – extended regular expressions (ERE). Those metacharacters work unescaped.
- fgrep – fixed strings. It treats the pattern as a literal string, so no regex metacharacters at all. Use it when searching for a literal dollar sign or asterisk.
In practice, most users prefer egrep for its readability. The only reason to use basic grep is if you are writing a script that must run on very old systems where egrep might not exist, but that is extremely rare today. Fgrep is still useful for searching for strings like “C++” or “a+b” without escaping.
Frequently Asked Questions
How do I search for multiple words with egrep?
Use the pipe alternation inside parentheses: egrep 'word1|word2|word3' file. This matches lines containing any of those words. If you want whole-word matches, add -w.
What is the difference between egrep and grep -E?
There is no difference – egrep is the same as grep -E on all modern systems. The egrep binary is kept for backward compatibility, but its man page explicitly says to use grep -E instead. Both interpret the pattern as an extended regular expression.
How do I show only the matching part, not the whole line?
Use the -o option. For example, egrep -o '[0-9]+' file prints only the numbers, one per line. Combine with wc -l to count total matches.
Can egrep search recursively through directories?
Yes, use the -r flag: egrep -r 'pattern' /path/to/dir. It will search all files in that directory and its subdirectories. To include hidden files, add --include='*' or use grep -r with the same flag.
Why does my egrep pattern with a pipe not work?
Most likely because you forgot to quote the pattern. The shell sees the | and treats it as a pipe between commands. Always write egrep 'a|b', not egrep a|b.
How do I match a literal dollar sign or asterisk with egrep?
Escape the metacharacter with a backslash: egrep '\$' file matches a dollar sign, and egrep '\*' file matches an asterisk. Alternatively, use fgrep to treat the whole pattern as literal.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Connect Mac to TV: Wired & Wireless Ways
- DDR4 RAM Prices Rising: Are These CPUs Still Worth Buying?
- Over Ear vs On Ear Headphones: Key Differences
- Best Family Streaming Plans: Top Picks & Guide
- Mercedes SUVs You Should Buy Used
- Japanese Hybrid Family Sedan vs SUV: Worth It?