sed Commands

sed commands cheatsheet.

Find-and-replace and line surgery from the command line. Tap any command to copy it.

Substitute (find & replace)
sed 's/old/new/' file.txtReplace the first match on each line
sed 's/old/new/g' file.txtReplace all matches on each line
sed 's/old/new/gi' file.txtReplace all, case-insensitive
sed -i 's/old/new/g' file.txtEdit the file in place
sed -i.bak 's/old/new/g' file.txtEdit in place, keep a .bak backup
sed 's|/old/path|/new/path|g' fileUse | as the delimiter for paths
Print, delete & select
sed -n '5p' file.txtPrint only line 5
sed -n '5,10p' file.txtPrint lines 5 through 10
sed '5d' file.txtDelete line 5
sed '/pattern/d' file.txtDelete every line matching a pattern
sed '$d' file.txtDelete the last line
sed -n '/start/,/end/p' file.txtPrint a range between two patterns
Insert, append & tidy
sed 's/^/ /' file.txtIndent every line by four spaces
sed 's/[[:space:]]*$//' file.txtStrip trailing whitespace
sed '/^$/d' file.txtRemove blank lines
sed '3i\new line' file.txtInsert a line before line 3
sed '3a\new line' file.txtAppend a line after line 3
sed -e 's/a/b/' -e 's/c/d/' fileApply several edits in one pass
By default sed prints to the screen and leaves your file untouched — add -i to edit in place. On macOS, -i requires an argument: use sed -i '' 's/old/new/g' file for no backup.

The substitute command does most of the work

sed is a "stream editor" — it reads text line by line and applies edits. The one command you'll use ninety percent of the time is substitute: s/old/new/ replaces text, and adding the g flag makes it replace every occurrence on a line instead of just the first. So sed 's/colour/color/g' fixes every instance on every line. The delimiter doesn't have to be a slash — when you're editing file paths, s|/old|/new| avoids a mess of escaped slashes.

Editing files in place, safely

By default sed prints the result and leaves the original file alone, which is great for previewing. To actually change the file, add -i (in place). The safe habit is -i.bak, which edits the file but first saves the original as file.bak — cheap insurance against a regex that matched more than you intended. A subtle gotcha: macOS (BSD) sed needs an explicit backup suffix, even an empty one, written as sed -i '' '...', whereas GNU sed on Linux is happy with a bare -i.

Working with specific lines

Beyond search-and-replace, sed can target lines by number or pattern. sed -n '5,10p' prints just lines 5 to 10 (the -n suppresses the default output so only the printed lines show), sed '5d' deletes line 5, and sed '/pattern/d' deletes every line matching a pattern — a quick way to strip blank lines or comments. For column-oriented data and arithmetic rather than line edits, reach for awk; to find the lines first, combine sed with grep.

FAQ

How do I find and replace text in a file with sed?
Use sed -i 's/old/new/g' file.txt. The s is substitute, g replaces all matches on each line, and -i writes the change back to the file. Use -i.bak to keep a backup first.
What does the g flag do in sed?
Without g, sed's substitute replaces only the first match on each line; with g it replaces every match on the line. Add i (as in s/old/new/gi) to also ignore case.

More cheatsheets