Most trouble with grep comes from one fact: it speaks three dialects of regular expression, and the default one treats |, + and ? as ordinary characters. A pattern copied from a tutorial fails silently, returns nothing, and exits. The examples below ran on GNU grep 3.11 against a ten-line sample log, with the exit status shown where it matters.
The sample file
Everything is searched in access.log:
192.168.1.10 - - [12/Mar/2026:09:14:02] "GET /index.html HTTP/1.1" 200 5120
192.168.1.22 - - [12/Mar/2026:09:14:07] "POST /login HTTP/1.1" 302 0
10.0.0.5 - - [12/Mar/2026:09:15:44] "GET /admin HTTP/1.1" 403 199
192.168.1.10 - - [12/Mar/2026:09:16:01] "GET /missing.png HTTP/1.1" 404 0
10.0.0.5 - - [12/Mar/2026:09:17:30] "GET /api/v2/users HTTP/1.1" 500 87
# maintenance window starts
203.0.113.77 - - [12/Mar/2026:09:20:11] "GET /index.html HTTP/1.1" 200 5120
ERROR: disk at 91%
error: retry scheduled
Line 8 is blank on purpose.
Three dialects, one default
Basic, extended, Perl
The GNU manual lists the options that choose a dialect. -G interprets patterns as basic regular expressions and is the default. -E switches to extended regular expressions. -P uses Perl-compatible ones, which the manual says work only if PCRE is available on the system. A fourth option, -F, turns the whole machinery off and treats the pattern as a fixed string.
The alternation that returns nothing
Here is the classic failure, a search for lines with either method:
$ grep 'GET|POST' access.log
$ echo $?
1
No output, exit status 1. Nothing is wrong with the file. In a basic regular expression, the manual explains, the characters ?, +, {, |, ( and ) lose their special meaning. The command looked for the literal seven-character text GET|POST.
Two fixes give the same six lines:
grep -E 'GET|POST' access.log
grep 'GET\|POST' access.log
The first changes dialect. The second stays in basic mode and backslashes the bar to give it back its power. Reaching for -E whenever a pattern has alternation, grouping or repetition counts keeps the backslashes out.
egrep is on its way out
Older scripts call egrep and fgrep. According to the manual both were deemed obsolescent by POSIX in 1992, dropped from the standard in 2001, and deprecated by GNU grep, with removal planned. grep -E and grep -F are the replacements.

Options that change what comes back
Count, number, invert
$ grep -c 'GET' access.log
5
-c suppresses the lines and prints a count of matching lines. -n prefixes each line with its line number, and -i ignores case:
$ grep -n -i 'error' access.log
9:ERROR: disk at 91%
10:error: retry scheduled
-v inverts the match. Combined with anchors it strips comments and blank lines in one pass:
grep -v -E '^(#|$)' access.log
That returned eight lines: the six requests and the two error messages. The caret matches the empty string at the start of a line and the dollar sign at the end, so ^# is a line starting with a hash and ^$ is a line with nothing in it.
Only the part that matched
-o prints just the matched text, each match on its own line. This is what turns grep from a filter into an extractor. Pulling the client address off the front of every request:
$ grep -o -E '^[0-9]+(\.[0-9]+){3}' access.log | sort | uniq -c | sort -rn
2 192.168.1.10
2 10.0.0.5
1 203.0.113.77
1 192.168.1.22
The pattern reads as: one or more digits, then exactly three repeats of a dot followed by digits. The {3} is an interval expression. {n,} means n or more and {n,m} between n and m.
Brackets, ranges and classes
A set of characters
Square brackets match one character from a set. Client errors 403 and 404:
$ grep ' 40[34] ' access.log
Two lines. Spaces inside the quotes keep the pattern from matching 403 inside a timestamp or a byte count. Server errors of any kind use a range and an interval:
$ grep -E ' 5[0-9]{2} ' access.log
10.0.0.5 - - [12/Mar/2026:09:17:30] "GET /api/v2/users HTTP/1.1" 500 87
Named classes need two sets of brackets
POSIX classes such as [:digit:], [:alpha:], [:upper:] and [:space:] are meant to be used inside a bracket expression, which means double brackets:
$ grep -E '^[[:upper:]]+:' access.log
ERROR: disk at 91%
Leave off the outer pair and grep refuses to guess:
$ grep '^[:upper:]' access.log
grep: character class syntax is [[:space:]], not [:space:]
$ echo $?
2
The classes are worth the extra typing. The manual warns that range behaviour such as [a-z] depends on the locale and is unspecified outside the C locale, and recommends setting LC_ALL=C when traditional ASCII ordering is wanted.
The unescaped dot matches for the wrong reason
A dot in a regular expression matches any single character. An IP address typed as a pattern is full of them. Against a two-line test file:
$ grep '192.168.1.10' dots.txt
192x168y1z10
192.168.1.10
The first line is not an address and it matched anyway. On real logs this error hides, because genuine addresses match too and the output looks right.
$ grep -F '192.168.1.10' dots.txt
192.168.1.10
-F is the cleaner fix than escaping every dot. When the thing being searched for is plain text, a host name, a file path, an error string with brackets in it, fixed-string mode means no character needs second-guessing.

Whole words and repeated words
-w and word boundaries
Searching for cat in the line cat concatenate cat:
$ grep -o 'cat' words.txt
cat
cat
cat
$ grep -o -w 'cat' words.txt
cat
cat
Three hits without -w, because concatenate contains one. -w selects only matches that form whole words. Inside a pattern the same idea is written \b for a word edge, or \< and \> for the start and end of a word. \w stands for a word character, which the manual defines as a synonym for [_[:alnum:]].
Back-references
A parenthesized group can be referred to later in the same pattern. \1 matches whatever the first group matched, which makes doubled words easy to catch:
$ grep -E '\b(\w+) \1\b' words.txt
the the quick fox
The manual notes that back-references can slow matching considerably. On a ten-line file nobody will notice. On a multi-gigabyte log they will.
An optional character is marked with ?. grep -E 'colou?r' matches both spellings.
Two more options round out the set. -x selects only matches that span the whole line, which suits checking a file of exact values such as usernames or host names. -e supplies a pattern explicitly and can be repeated, so grep -e 'GET' -e 'POST' is a third way to write the alternation from the first section, and it works in every dialect.
Context and recursive search
Lines around the match
A matching line alone is often not enough to explain an error. -A prints lines after each match, -B lines before, -C both:
$ grep -B1 -A1 ' 500 ' access.log
192.168.1.10 - - [12/Mar/2026:09:16:01] "GET /missing.png HTTP/1.1" 404 0
10.0.0.5 - - [12/Mar/2026:09:17:30] "GET /api/v2/users HTTP/1.1" 500 87
# maintenance window starts
Whole directory trees
-r searches directories recursively. With -n each hit comes back as file, line number, text:
$ grep -rn 'TODO' .
./src/main.c:1:TODO fix parser
./vendor/lib.c:1:TODO vendored
$ grep -rn --exclude-dir=vendor 'TODO' .
./src/main.c:1:TODO fix parser
--exclude-dir skips directories whose names match a glob, and --include='*.c' limits the search to matching file names. -l prints only the names of files that contain a match, which is the form to pipe into another command.
The manual draws one distinction here that is easy to miss. -r skips symbolic links it meets while recursing. -R follows all of them.

Exit status: grep as a test
0, 1 and 2
grep reports through its exit status as well as its output. The manual gives three values: 0 when a line was selected, 1 when none was, 2 when an error occurred. A missing file shows the last one:
$ grep 'x' nofile.txt
grep: nofile.txt: No such file or directory
$ echo $?
2
That is why the failed alternation near the top was worth checking with echo $?. Status 1 said the pattern was valid and matched nothing, which pointed at the dialect.
-q for scripts
-q prints nothing and exits with status 0 as soon as any match is found, which makes it the natural condition for an if:
grep -q ' 500 ' access.log && echo "found a 500"
found a 500
When -P earns its place
**Lookahead and
Perl mode adds constructs the other two dialects lack. A lookahead matches a position without consuming text. This pulls the status code out of each request by asking for three digits that are followed by a space and the final number on the line:
$ grep -P -o '\d{3}(?= \d+$)' access.log
200
302
403
404
500
200
The shorthand \d belongs to Perl mode, and using it elsewhere fails in a way that is easy to miss. A two-line file holding abc 123 and ddd shows it:
$ grep -E '\d{3}' dtest.txt
ddd
$ grep -P '\d{3}' dtest.txt
abc 123
Under -E the pattern matched three letter d’s and ignored the digits. No error, exit status 0. [0-9] or [[:digit:]] works in all three dialects.
Sources
- GNU Grep manual (single-page edition): https://www.gnu.org/software/grep/manual/grep.html
