sed Cheatsheet - Linux Text Editing Reference
All essential sed commands organized by use case, with 37+ entries you can copy and run directly. Find the right command fast when you need it.
Back to SysOpsSubstitute 8
sed 's/old/new/' fileReplace first match per line
sed 's/old/new/g' fileReplace all matches (global)
sed 's/old/new/2' fileReplace the 2nd match per line
sed 's/old/new/I' fileCase-insensitive match
sed -i 's/old/new/g' fileEdit the file in place
sed -i.bak 's/old/new/g' fileEdit and back up the original
sed 's/\/path\/old/path\/new/g' fileEscape slashes when replacing paths
sed 's|old|new|g' fileUse | as delimiter to avoid escaping slashes
Delete 7
sed '5d' fileDelete line 5
sed '5,10d' fileDelete lines 5-10
sed '/pattern/d' fileDelete matching lines
sed '/^$/d' fileDelete blank lines
sed '/^#/d' fileDelete comment lines
sed '$d' fileDelete the last line
sed '1d;$d' fileDelete the first and last lines
Insert & Append 5
sed '5i\new text' fileInsert before line 5
sed '5a\new text' fileAppend after line 5
sed '/pattern/i\new text' fileInsert before matching lines
sed '/pattern/a\new text' fileAppend after matching lines
sed '5i\line1\nline2' fileInsert multiple lines
Print 5
sed -n '5p' filePrint only line 5
sed -n '5,10p' filePrint only lines 5-10
sed -n '/pattern/p' filePrint only matching lines
sed -n '1p;$p' filePrint first and last lines
sed 's/old/new/p' fileSubstitute and print changed lines
Regular Expression 7
sed 's/^prefix/new/' fileMatch line start
sed 's/suffix$/new/' fileMatch line end
sed 's/\<word\>/new/g' fileMatch word boundary
sed 's/[0-9]/X/g' fileMatch digits
sed 's/[^0-9]/X/g' fileMatch non-digits
sed 's/\(pattern\)/\1/g' fileCapture group and backreference
sed -E 's/(old)/\1-new/g' fileExtended regex (-E or -r)
Multi-line 5
sed ':a;N;$!ba;s/\n//g' fileDelete all newlines
sed 'N;s/\n/ /'Merge adjacent two lines
sed '/start/,/end/s/old/new/g' fileSubstitute within a range
sed '/start/,/end/d' fileDelete lines within a range
sed '/start/,/end/p' filePrint lines within a range
Tips
- Before editing in place with sed -i, back up first (-i.bak), or preview without -i.
- sed uses BRE (basic regex) by default; +, ?, | must be escaped or enable ERE with -E.
- On macOS, sed -i requires a backup suffix (e.g. -i ""); on Linux -i works directly.
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us