OnCallReady

Commands

grep - print lines matching a pattern

grep [options] PATTERN [file]...

Options you will use

-i, --ignore-case
Case-insensitive match.
-v, --invert-match
Select the lines that do NOT match.
-c, --count
Print only the count of matching lines.
-n, --line-number
Prefix each line with its line number.
-r, -R, --recursive
Search directories recursively.
-l, --files-with-matches
Print only the names of matching files.
-E, --extended-regexp
Use ERE: + ? | ( ) without backslashes.
-F, --fixed-strings
Treat the pattern as a literal string.
-P
Perl-compatible regex (\d, \s, lookarounds).
-o, --only-matching
Print only the matched part of the line.
-A N, -B N, -C N
Print N lines after / before / around each match.
-w
Match whole words only.
-q
Quiet: exit 0 on the first match, print nothing. For if-tests.
-m, --max-count NUM
Stop after NUM matching lines: -m1 is "the first match only".
-Z, --null
After a file name print a NUL byte instead of a newline (pairs with xargs -0).
-q, --quiet
Print nothing; exit 0 at the first match. For if grep -q ....
-x, --line-regexp
Match only whole lines.
-F, --fixed-strings
The pattern is plain text, not a regular expression.

Examples

$ grep -c ' 502 ' /var/log/nginx/access.log

how many 502s

$ sudo grep -B2 -A2 'upstream timed out' /var/log/nginx/error.log

the lines around each timeout

$ grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' access.log | sort | uniq -c | sort -rn | head

top talkers

Taught in

Try grep in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.