sed command examples: find and replace
Updated · Every command on this page ran in our practice shell
Short answer
sed 's/old/new/' file prints the file with the first old on each line replaced by new. Add g (s/old/new/g) to replace every match, and -i to change the file itself. sed -n '3p' prints line 3 and sed '/^#/d' deletes comment lines.
sed is the stream editor: it reads text line by line, changes it, and prints the result. It's the quickest way to fix a typo in a config file or rename something across a document without opening an editor. Every example below ran on a small website's config files.
Replace text with s/old/new/
sed 's/old/new/' file prints the file with the first match on each line replaced. The file itself isn't changed, so this doubles as a preview; sed -n 's/old/new/p' file prints only the lines that would change. Always put the sed script in single quotes.
learner@shellstreak:~/bakery$ sed 's/Bakary/Bakery/' config/site.conf # Pebble Bakery website settings site_name=Pebble Bakery host=localhost port=8080 # cache time in seconds cache_ttl=300 debug=true
Replace every match with g
Without a flag, s stops after the first match on each line. Add g (global) at the end to replace all of them.
learner@shellstreak:~/bakery$ echo "rye rolls, rye toast, rye crackers" | sed 's/rye/spelt/g' spelt rolls, spelt toast, spelt crackers
Edit a file in place with -i
sed -i writes the result back into the file. There's no undo, so preview without -i first, name the exact file rather than a broad glob, and consider sed -i.bak, which keeps the original as file.bak. On macOS (BSD sed) the flag needs an argument: sed -i '' 's/a/b/' file.
learner@shellstreak:~/bakery$ sed -i 's/Bakary/Bakery/' config/site.conf && grep site_name config/site.conf site_name=Pebble Bakery
Print only some lines with -n and p
-n turns off sed's normal printing, and the p command prints the lines you choose: a line number ('4p'), a range ('2,4p') or the lines matching a pattern ('/debug/p').
learner@shellstreak:~/bakery$ sed -n '2,4p' config/site.conf site_name=Pebble Bakary host=localhost port=8080
Delete lines with d
/pattern/d deletes matching lines from the output. ^ anchors the pattern to the start of the line, so '/^#/d' strips comment lines. '3d' deletes line 3 and '/^$/d' deletes empty lines.
learner@shellstreak:~/bakery$ sed '/^#/d' config/site.conf site_name=Pebble Bakary host=localhost port=8080 cache_ttl=300 debug=true
Patterns: ^, $, . and .*
sed patterns are regular expressions. ^ is the start of a line, $ the end, . any one character and .* any run of characters, so sed 's/^host=.*/host=localhost/' file replaces a whole host= line and sed '/^$/d' file deletes empty lines. A backslash makes a special character plain: sed 's/3\.50/3.75/' file matches a real dot.
Several edits at once, and other delimiters
Give sed several scripts with -e. When the text contains slashes, like a path, use another delimiter such as |: s|/old|/new| means the same as s/\/old/\/new/ but is much easier to read.
learner@shellstreak:~/bakery$ sed -e 's/debug=true/debug=false/' -e 's/8080/9090/' config/staging.conf host=staging.example.com port=9090 debug=false
When tr is simpler
For swapping or deleting single characters, tr is shorter: tr a-z A-Z uppercases, tr -d ',' deletes commas and tr -s ' ' squeezes repeated spaces. It only reads standard input, so feed it with < or a pipe.
learner@shellstreak:~/bakery$ tr a-z A-Z < data/items.txt SOURDOUGH RYE CROISSANT
Join files and lines with paste
paste a.txt b.txt joins two files line by line, with a tab between; paste -d, a.txt b.txt uses a comma instead. paste -sd, file puts every line of one file on a single line, separated by commas.
Sort the result by a column
sed often sits in a pipeline next to sort. sort -t, -k3,3n sorts comma-separated lines by the third column only, as numbers; -k3,3nr puts the largest first. Write -k3,3, not -k3: plain -k3 makes the key run from field 3 to the end of the line.
learner@shellstreak:~/bakery$ sort -t, -k3,3nr data/sales.csv | head -n 3 2026-03-02,croissant,12,3 2026-03-03,croissant,8,3 2026-03-03,sourdough,6,6
Check yourself
One real exercise from the “Text processing” unit. No account needed.Print specials.txt with Tomorrow replaced by Friday.
What's checked: the output of your last command.
Common mistakes
Forgetting
gand wondering why only the first match on each line changed.Using double quotes around the script, so the shell replaces
$1or$HOMEbefore sed sees them. Use single quotes.Running
sed -ion a glob likeconfig/*.confwhen you meant one file. Every matching file changes, with no undo.Forgetting that
.and*are special in patterns.s/1.0/2.0/also matches1x0; write1\.0for a literal dot.
Questions
How do I replace a word in a file from the command line?+
`sed -i 's/old/new/g' file.txt` replaces every `old` with `new` in the file itself. Run it without `-i` first to preview the result.
How do I make a backup while editing in place?+
With GNU sed, `sed -i.bak 's/old/new/' file` saves the original as `file.bak` before changing it. On macOS, `sed -i .bak 's/old/new/' file` does the same.
What's the difference between sed and awk?+
sed is best at editing lines: substitutions, deletions, printing ranges. awk is best at columns: picking fields, filtering on their values and adding them up.
Reading is step one. Now make it stick.
Practise this in a five-minute lesson with instant feedback. No account needed.