Show Menu
Cheatography

Grep / Sed Cheat Sheet (DRAFT) by [deleted]

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Pipeline Commands

sort
Sorts the contents of a text file
head [-n=num]
Prints the first num or 10 lines of the text file
tail [-n=num]
Prints the last num or 10 lines of the text file

Sed Commands

sed -r 'action'
Apply action to all lines
sed -r 'address action'
Apply action to lines that match address
sed -r 'addre­ss!­action'
Apply action to lines that don't match address

Grep Commands

grep -E "..."
Extended RegEx Search Pattern
 

Sed Address Commands

blank
Match everything
line number
Match specific line
$
Match last line of file
/regexp/
Match lines that match regex
line1,line2
Match lines between the limits
/regexp1/,/regexp2/
Match lines between the two regex occurances
line1,/regexp1/
Matches all lines between line1 and the regex
/regexp2/,line2
Matches all lines between the regex and line2

Sed Action Commands

p
Print the line
d
Delete the line
s/regex/string/
Change the first match of regex on the line to string
s/regex/string/2
Change the second match of regex on the line to string
s/regex/string/g
Change all matches of regex to string
s/regex/string/i
Case insens­itive. Change the first match of regex on the line to string
y/string1/string2/
Transl­iterate corres­ponding characters
istring
Insert a line before the current line
astring
Insert a line after the current line
Within a replac­ement string you can use:
& - represents the matched regex
\U \L \E \u \l - to do case conversion
\1 \2 etc - to represent brackets in the regex