grep (english) Cheat Sheet

A Software › Linux cheat sheet in English by TME520

A quick reminder about grep : most common options, regular expressions, wildcards, escaping characters...

55,557 views 11,715 downloads 4 May 2015 History
4.0 avg · 8 ratings

Usage

Grep standard output (i.e. a stream of text)
grep [-options] ‘string’
Grep the content of a file
grep [-options] ‘string’ filename
Wildcards are accepted in filename.

General Regular Expression Processor

Operation
Option
Example
Find a string in 1 or more files
 
grep '­str­ing­' filename1 filename2 ... filenamen
Case insens­itive search
i
grep -i '­str­ing­' filename
Use regular expres­sions (regex)
 
grep '­reg­ex' filename
Look for words
w
grep -w '­wor­d' filename
Display n lines after matching string
A
grep -A n '­str­ing­' filename
Display n lines before matching string
B
grep -B n '­str­ing­' filename
Display n lines around matching string
C
grep -C n '­str­ing­' filename
Recursive grep
r
grep -r '­hac­ker­s-c­lub.cn­' /var/l­­og­/­a­pa­­che­­2/­a­r­ch­­ives/
Return all lines which don't match the pattern
v
grep -v '­war­nin­g' /var/l­og/­syslog
Use regex
e
grep -e '­str­ing­1' -e '­str­ing­2' filename
   
grep --regexp '­str­ing­' filename
Return lines starting with '­al'
 
grep -e '­^al­' filename
Use extended regex
E
grep -E '­apa­che­|wh­eel­|ro­ot' 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|c­ks)­on' filename
Count results
c
grep -c '­err­or' /var/l­og/­syslog
Display filename
l
grep -l '­str­ing­' /var/log/*
Only show the matching part of the string
o
grep -o '­str­ing­' filename
Show line number
n
grep -n '­str­ing­' filename
About grep -E: In basic regular expres­sions the meta-c­har­acters ‘?’, ‘+’, ‘{’, ‘|’, ‘(’, and ‘)’ lose their special meaning; instead use the backsl­ashed versions ‘\?’, ‘\+’, ‘\{’, ‘\|’, ‘\(’, and ‘\)’.

GNU grep -E emulates classic meta-c­har­acters. The command ‘grep -E '­{1'’ searches for the 2-char­acter string ‘{1’ instead of reporting an error. POSIX allows this behavior as an extension, but portable scripts should avoid it.

Regular expres­sions : wildcards

.
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 whites­pace.
[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.

Regular expres­sions : anchors and positions

^
Beginning of line.
grep '­^Once upon a time' /home/­­li­v­r­es­­/lo­­ve­s­t­or­­y.txt
$
End of line.
grep '­div­or­­ced.$' /home/­­li­v­r­es­­/lo­­ve­s­t­or­­y.txt
^$
Empty line.
\<
Start of word.
grep '­\<l­ove­\>' /home/­­ma­nga­/se­ine­n.txt
\>
End of word.

Characters to escape

\
.
[
^
$
'
*
- (start of line only)

Badges

Useful Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 1

Comments (3)

Awfki 20:05 4 Apr 2017

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 "­Joh­n" and "­Jam­es" indepe­ndently from the case E and i and w grep -E -i -w 'john|­james' /etc/p­­asswd

Reply
TME520 00:33 7 Sep 2017

Thanks for your feedback. True, I need to make this clearer. Watch out for the next update.

Reply
TME520 07:15 27 Sep 2017

I updated the cheat sheet, I hope it's better now.

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.