A quick reminder about grep : most common options, regular expressions, wildcards, escaping characters...
|
Grep standard output (i.e. a stream of text)
grep [-options] ‘string’
|
|
Grep the content of a file
grep [-options] ‘string’ filename
|
|
Operation
|
Option
|
Example
|
|
Find a string in 1 or more files
|
grep 'string' filename1 filename2 ... filenamen
|
|
|
Case insensitive search
|
i
|
grep -i 'string' filename
|
|
Use regular expressions (regex)
|
grep 'regex' filename
|
|
|
Look for words
|
w
|
grep -w 'word' filename
|
|
Display n lines after matching string
|
A
|
grep -A n 'string' filename
|
|
Display n lines before matching string
|
B
|
grep -B n 'string' filename
|
|
Display n lines around matching string
|
C
|
grep -C n 'string' filename
|
|
Recursive grep
|
r
|
grep -r 'hackers-club.cn' /var/log/apache2/archives/
|
|
Return all lines which don't match the pattern
|
v
|
grep -v 'warning' /var/log/syslog
|
|
Use regex
|
e
|
grep -e 'string1' -e 'string2' filename
|
|
grep --regexp 'string' filename
|
||
|
Return lines starting with 'al'
|
grep -e '^al' filename
|
|
|
Use extended regex
|
E
|
grep -E 'apache|wheel|root' filename
|
|
Get lines containing 1+ w
|
grep -E 'w+' filename
|
|
|
Get lines with 3 w in a row (www)
|
grep -E 'w{3}' filename
|
|
|
Get lines containing between 3 and 6 m in a row
|
grep -E 'm{3,6}' filename
|
|
|
Get lines containing jason or jackson
|
grep -E 'ja(s|cks)on' filename
|
|
|
Count results
|
c
|
grep -c 'error' /var/log/syslog
|
|
Display filename
|
l
|
grep -l 'string' /var/log/*
|
|
Only show the matching part of the string
|
o
|
grep -o 'string' filename
|
|
Show line number
|
n
|
grep -n 'string' filename
|
|
.
|
Any character.
|
|
?
|
Optional and can only occur once.
|
|
*
|
Optional and can occur more than once.
|
|
+
|
Required and can occur more than once.
|
|
{n}
|
Previous item appears exactly n times.
|
|
{n,}
|
Previous item appears n times or more.
|
|
{,m}
|
Previous item appears n times maximum.
|
|
{n,m}
|
Previous item appears between n and m times.
|
|
[:alpha:]
|
Any lower and upper case letter.
|
|
[:digit:]
|
Any number.
|
|
[:alnum:]
|
Any lower and upper case letter or digit.
|
|
[:space:]
|
Any whitespace.
|
|
[A-Za-z]
|
Any lower and upper case letter.
|
|
[0-9]
|
Any number.
|
|
[0-9A-Za-z]
|
Any lower and upper case letter or digit.
|
|
^
|
Beginning of line.
|
grep '^Once upon a time' /home/livres/lovestory.txt
|
|
$
|
End of line.
|
grep 'divorced.$' /home/livres/lovestory.txt
|
|
^$
|
Empty line.
|
|
|
\<
|
Start of word.
|
grep '\<love\>' /home/manga/seinen.txt
|
|
\>
|
End of word.
|
|
|
\
|
.
|
|
[
|
^
|
|
$
|
'
|
|
*
|
- (start of line only)
|
The example below references -E but that's not one of the explained options. According to man page it enables RegEx so that grep behaves like egrep. Find "John" and "James" independently from the case E and i and w grep -E -i -w 'john|james' /etc/passwd
Thanks for your feedback. True, I need to make this clearer. Watch out for the next update.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.