What Is sed and How Does It Work? (Stream Editor)

sed (stream editor) transforms text non-interactively based on a set of rules, most commonly for search-and-replace.

Published September 30, 2026

sed is a stream editor that transforms text line by line, most commonly to find and replace with s/old/new/. It reads input, applies commands and prints the result.

How it works

By default sed prints to stdout and leaves files untouched; -i edits in place (on macOS use -i '').

Commands can also delete lines, print ranges or insert text.

Try it

echo "hello world" | sed 's/world/Linux/'
hello Linux

Practical examples

Replace all occurrences

sed 's/foo/bar/g' file.txt

Edit in place

sed -i 's/localhost/db.internal/g' config.ini

Delete lines matching a pattern

sed '/^#/d' config.conf

Print a line range

sed -n '10,20p' file.txt

Common mistakes

  • Forgetting the g flag so only the first match per line changes.
  • Using / in patterns containing paths (use another delimiter like |).
  • Editing in place without a backup (-i.bak).

Key takeaways

  • s/old/new/g replaces text.
  • -i edits files.
  • Use another delimiter for paths.

FAQ

sed vs awk?

sed edits text streams; awk processes fields and does calculations.

macOS differences?

BSD sed needs -i '' for in-place edits.

More Linux articles