Skip to main content

sed Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for sed stream editor

Sed is a stream editor for filtering and transforming text. This cheatsheet covers common editing tasks, addressing, substitutions, and in-place edits.

Syntax

General command forms.

CommandDescription
sed 'script' fileRun sed script on a file
sed -n 'script' fileSuppress auto-print, print only with p
printf '%s\n' "text" | sed 'script'Read from stdin
sed -e 'cmd1' -e 'cmd2' fileMultiple commands
sed -f script.sed fileRead commands from file

Options

Common CLI flags.

CommandDescription
sed -n 'script' fileSuppress auto-print
sed -E 'script' fileExtended regex (GNU and BSD)
sed -r 'script' fileExtended regex (GNU only)
sed -i 'script' fileEdit in place (GNU sed)
sed -i.bak 'script' fileEdit in place with backup

Substitution

Replace text with s/old/new/.

CommandDescription
sed 's/old/new/' fileReplace first match on each line
sed 's/old/new/g' fileReplace all matches
sed 's/old/new/2' fileReplace second match
sed 's/old/new/Ig' fileCase-insensitive replace (GNU sed)
sed 's|/usr|/opt|g' fileUse alternate delimiter
sed -n 's/old/new/p' filePrint only lines with replacements
sed 's/old/new/w out.txt' fileWrite changed lines to file

Addresses

Apply commands to specific lines.

CommandDescription
sed '3s/a/b/' fileSubstitute on line 3 only
sed '1,5s/a/b/' fileLines 1 through 5
sed '/pattern/s/a/b/' fileLines matching pattern
sed '3,/pattern/s/a/b/' fileLine 3 through first match
sed '/start/,/end/d' fileDelete range between patterns

Control output and remove lines.

CommandDescription
sed -n 'p' filePrint all lines (same as cat)
sed -n '3p' filePrint line 3 only
sed -n '/pattern/p' filePrint matching lines
sed -n '1,5p' filePrint range of lines
sed 'd' fileDelete all lines (prints nothing)
sed '3d' fileDelete line 3
sed '/pattern/d' fileDelete matching lines

Insert, Append, Change

Add or replace whole lines.

CommandDescription
sed '2i\\new line' fileInsert before line 2
sed '2a\\new line' fileAppend after line 2
sed '2c\\new line' fileReplace line 2
sed '/pattern/i\\new line' fileInsert before matches
sed '/pattern/a\\new line' fileAppend after matches

Other Commands

Useful non-substitution commands.

CommandDescription
sed 'y/abc/xyz/' fileTranslate characters
sed '=' filePrint line numbers
sed 'q' fileQuit after first line
sed '3q' fileQuit after line 3
sed 'n' fileRead next line, skip current output
sed 'N' fileAppend next line to pattern space
sed '/pattern/r other.txt' fileRead file after matches
sed '/pattern/w out.txt' fileWrite matching lines to file

In-Place Editing

Write changes back to files.

CommandDescription
sed -i 's/old/new/g' fileEdit in place (GNU sed)
sed -i.bak 's/old/new/g' fileEdit in place with backup
sed -i '' 's/old/new/g' fileEdit in place on macOS/BSD
sed -i -e 's/a/b/' -e 's/c/d/' fileMultiple edits in place

Regular Expressions

Use regex and capture groups.

CommandDescription
sed 's/[0-9]\{4\}/YEAR/' fileReplace 4-digit numbers
sed -E 's/[0-9]{4}/YEAR/g' fileExtended regex (GNU and BSD)
sed 's/\(foo\)bar/\1baz/' fileCapture group (basic)
sed -E 's/(foo)bar/\1baz/' fileCapture group (extended)
sed -E 's#(https?)://#\1://#' fileUse groups and alternate delimiter

Line Selection

Select by position or pattern.

CommandDescription
sed -n '1p' fileFirst line
sed -n '$p' fileLast line
sed -n '1~2p' fileEvery 2nd line (GNU sed)
sed -n '/error/,+2p' fileMatch plus next 2 lines
sed -n '/start/,/end/p' filePrint between patterns

Common Patterns

Useful one-liners.

CommandDescription
sed 's/[[:space:]]\+$//' fileTrim trailing whitespace
sed 's/^[[:space:]]\+//' fileTrim leading whitespace
sed 's/[[:space:]]\+/ /g' fileCollapse whitespace
sed '/^#/d;/^$/d' fileRemove comments and blank lines
sed -n 'n;p' filePrint even lines