Sed Cheat Sheet

Basic Commands

CommandDescriptionExample
pPrint the current linesed -n '2p' file.txt
dDelete the current linesed '3d' file.txt
qQuit after the first matchsed '/pattern/q' file.txt
nRead the next line into pattern spacesed 'n;s/old/new/' file.txt
NAppend the next line to pattern spacesed 'N;s/\n/ /' file.txt

Line Addressing

CommandDescriptionExample
1,3dDelete lines 1 to 3sed '1,3d' file.txt
2pPrint the second linesed -n '2p' file.txt
5,$dDelete from line 5 to the endsed '5,$d' file.txt
/pattern/dDelete lines containing patternsed '/error/d' file.txt
/pattern/!dDelete lines NOT matching patternsed '/keep/!d' file.txt
/#/dDelete lines containing a hash symbolsed '/#/d' file.txt

Substitution Commands

CommandDescriptionExample
s/old/new/Substitute first occurrencesed 's/old/new/' file.txt
s/old/new/gSubstitute all occurrences (global)sed 's/old/new/g' file.txt
s/old/new/2Substitute second occurrence onlysed 's/old/new/2' file.txt
s/old/new/iCase-insensitive substitutionsed 's/old/new/i' file.txt
s/old/new/pPrint line after substitutionsed 's/old/new/p' file.txt
1,3s/old/new/gSubstitute globally on lines 1 to 3sed '1,3s/old/new/g' file.txt

Pattern Matching

CommandDescriptionExample
s/^start/replace/Match at beginning of linesed 's/^start/replace/' file.txt
s/end$/replace/Match at end of linesed 's/end$/replace/' file.txt
s/\bword\b/replace/Match whole word onlysed 's/\bword\b/replace/' file.txt
s/\<word\>/replace/Alternative whole word matchsed 's/\<word\>/replace/' file.txt

Character Classes and Regex

CommandDescriptionExample
s/[0-9]/X/gReplace all digits with Xsed 's/[0-9]/X/g' file.txt
s/[^0-9]//gRemove non-numeric characterssed 's/[^0-9]//g' file.txt
s/[aeiou]//gDelete all vowelssed 's/[aeiou]//g' file.txt
s/[^aeiou]//gDelete all consonantssed 's/[^aeiou]//g' file.txt
s/\<[0-9]\+\>//gRemove whole numberssed 's/\<[0-9]\+\>//g' file.txt

Backreferences and Groups

CommandDescriptionExample
s/\(.*\),\(.*\)/\2 \1/Swap comma-separated fieldssed 's/\(.*\),\(.*\)/\2 \1/' file.txt
s/\(pattern\)/\1/gBackreference matched patternsed 's/\([0-9]\)/[\1]/g' file.txt
s/\([0-9]\)/\1\1/gDuplicate each digitsed 's/\([0-9]\)/\1\1/g' file.txt
s/\(.\)/\1 /gAdd space after each charactersed 's/\(.\)/\1 /g' file.txt

Whitespace and Formatting

CommandDescriptionExample
`s/^[[:space:]]*//'Trim leading whitespacesed 's/^[[:space:]]*//' file.txt
`s/[[:space:]]*$//'Trim trailing whitespacesed 's/[[:space:]]*$//' file.txt
s/^[[:space:]]*//;s/[[:space:]]*$//Trim both leading and trailingsed 's/^[[:space:]]*//;s/[[:space:]]*$//' file.txt
s/\n/ /gReplace newlines with spacessed 's/\n/ /g' file.txt
s/ */ /gCollapse multiple spaces to onesed 's/ */ /g' file.txt

Case Conversion

CommandDescriptionExample
s/\(.*\)/\U\1/Uppercase entire linesed 's/\(.*\)/\U\1/' file.txt
s/\(.*\)/\L\1/Lowercase entire linesed 's/\(.*\)/\L\1/' file.txt
s/^\(.\)/\U\1/Uppercase first charactersed 's/^\(.\)/\U\1/' file.txt
s/^\(.\)/\L\1/Lowercase first charactersed 's/^\(.\)/\L\1/' file.txt

Comments and Text Manipulation

CommandDescriptionExample
s/^/# /Comment out lines with #sed 's/^/# /' file.txt
s/^[^#].*$/# &/Comment out non-comment linessed 's/^[^#].*$/# &/' file.txt
s/^# //Uncomment linessed 's/^# //' file.txt

Advanced Commands

CommandDescriptionExample
a\textAppend text after linesed '2a\New line' file.txt
i\textInsert text before linesed '2i\New line' file.txt
c\textChange/replace entire linesed '2c\Replacement line' file.txt
r filenameRead file and insert contentssed '2r other.txt' file.txt
w filenameWrite pattern space to filesed '/pattern/w output.txt' file.txt

Hold Space Operations

CommandDescriptionExample
hCopy pattern space to hold spacesed '1h;2g' file.txt
HAppend pattern space to hold spacesed '1h;2H;3g' file.txt
gCopy hold space to pattern spacesed '1h;3g' file.txt
GAppend hold space to pattern spacesed '1h;2G' file.txt
xExchange pattern and hold spacesed '1h;2x' file.txt

Special Commands

CommandDescriptionExample
y/abc/123/Transliterate characters a→1, b→2, c→3sed 'y/abc/123/' file.txt
lDisplay non-printing characterssed -n 'l' file.txt
=Print line numbersed -n '3=' file.txt
!commandRun shell commandsed '1!d' file.txt
;Command separatorsed 's/old/new/;s/foo/bar/' file.txt

Useful Examples

CommandDescriptionExample
sed -n '1,5p'Print lines 1 to 5sed -n '1,5p' file.txt
sed '1d;$d'Remove first and last linesed '1d;$d' file.txt
sed '/^$/d'Remove empty linessed '/^$/d' file.txt
sed 's/.*/"&"/'Quote entire linesed 's/.*/"&"/' file.txt
sed -n '/start/,/end/p'Print between patternssed -n '/start/,/end/p' file.txt