grep uses regular expressions to describe patterns instead of fixed words. By default it reads basic regular expressions (BRE); grep -E switches to extended regular expressions (ERE), and egrep is the old name for grep -E. For new work, write grep -E and use grep -P only when you need Perl-style features. These rules are what grep regular expressions are built on.
You should already know the basic grep PATTERN FILE form. This article adds the pattern language.
GNU grep supports three regex flavours:
| Flavour | Option | Notes |
|---|---|---|
| Basic (BRE) | default | + ? | ( ) { } need a backslash to be special |
| Extended (ERE) | -E | Those characters are special without a backslash |
| Perl (PCRE) | -P | Lookahead, \d, lazy quantifiers; GNU only, not always built in |
egrep is a wrapper for grep -E. It is deprecated: recent GNU grep versions print a warning when you run it, so use grep -E in new scripts. Likewise fgrep is grep -F.
Prepare sample data:
Anchors fix a match to a position: ^ is the start of a line and $ is the end. The dot . matches any single character.
The third command finds lines where @ is the sixth character: only [email protected] qualifies. To match a literal dot, escape it as \..
Square brackets match one character from a set.
| Pattern | Matches |
|---|---|
[abc] | a, b or c |
[a-z] | Any lowercase letter |
[^0-9] | Any character that is not a digit |
[[:digit:]] | A digit (POSIX class) |
[[:alpha:]] | A letter |
[[:space:]] | Whitespace |
[[:upper:]] | An uppercase letter |
POSIX classes such as [[:digit:]] are locale aware and more portable than ranges like [a-z], which can behave unexpectedly in some locales.
Quantifiers say how many times the previous item may repeat.
| Quantifier (ERE) | Meaning |
|---|---|
* | Zero or more |
+ | One or more |
? | Zero or one |
{3} | Exactly 3 |
{2,4} | Between 2 and 4 |
{2,} | 2 or more |
In BRE, * works as is, but the others need backslashes: grep '[0-9]\{4\}' is the BRE form of -E '[0-9]{4}'. This is the main reason people prefer -E.
In ERE, parentheses group parts of a pattern and | means "or".
The group chooses between example and mail.example, then the dot and the top-level domain are matched. Parentheses also capture text for back-references such as \1, which repeat what the group matched:
A simple, rough pattern for email-shaped lines is:
This is good for finding candidates in a file. It does not validate real addresses, which is a harder problem than a regex can solve.
GNU grep supports \< and \> for word start and end, and \b for a word boundary. Combine with -o to print only the match.
For PCRE features, such as \d and non-greedy matching, use -P:
-P is not available in every build and is not POSIX, so prefer -E in portable scripts.
egrep is the same as grep -E, but it is deprecated. Use grep -E.
In basic mode those characters are literal. Add -E, or escape them as \+ and \| in BRE.
Escape it with a backslash, for example \. or \$. Inside brackets most special characters are literal already: [.] matches a dot.
Unquoted characters like *, ?, ( and | are handled by the shell. Always wrap patterns in single quotes.
Basic regular expressions are the default in grep, and grep -E gives you the extended syntax with +, ?, {n}, groups and | without backslashes. Learn anchors, the dot, bracket classes, quantifiers and groups, quote patterns in single quotes, and use -o to extract matches. Treat egrep as legacy and -P as a convenient but non-portable extra.