sed is useful when I need to change text from the command line. It can replace words, delete matching lines, or print only the lines I care about.

It can also edit many files quickly, which means I do not run it carelessly.

The sed Replacement Builder helps build the command in small steps so I can understand the replacement before running it.

The basic replacement

A simple sed replacement looks like this:

sed 's/old/new/' file.txt

Plain meaning:

In each line, replace the first old with new, then print the result.

This does not change the file yet. It prints the changed version to the terminal. That is good for previewing.

Replace every match on a line

By default, sed replaces only the first match on each line.

Input:

old old old

Command:

sed 's/old/new/' file.txt

Output:

new old old

To replace every match on the line, add g:

sed 's/old/new/g' file.txt

Output:

new new new

The g means global for each line.

Use a different delimiter when replacing paths

The slash / is the common delimiter:

sed 's/old/new/g'

But if the text contains paths, slash becomes annoying:

sed 's/\/var\/www\/old/\/var\/www\/new/g' file.txt

A cleaner version uses another delimiter, such as #:

sed 's#/var/www/old#/var/www/new#g' file.txt

Same meaning, easier to read.

The builder is useful here because it can choose a delimiter that makes the command less messy.

Preview before editing the file

My rule is simple: preview first.

sed 's/example.com/api.example.com/g' config.txt

This shows what the file would look like after replacement, but it does not save the change.

Only after the output looks correct do I use in-place editing.

Edit in place with a backup

In-place editing changes the file. On many systems, this uses -i:

sed -i 's/example.com/api.example.com/g' config.txt

For safer editing, create a backup:

sed -i.bak 's/example.com/api.example.com/g' config.txt

This changes config.txt and keeps a backup as:

config.txt.bak

If the replacement was wrong, I can recover from the backup.

Replace only matching lines

Sometimes I only want to replace text on lines that contain another pattern.

Example:

sed '/API_URL/s/example.com/api.example.com/' .env

Plain meaning:

Only on lines containing API_URL, replace example.com with api.example.com.

This is safer than replacing example.com everywhere.

Delete matching lines

sed can also delete lines.

sed '/debug/d' app.log

Plain meaning:

Print the file but remove lines containing debug.

Again, this previews the result. It does not edit the file unless you add -i.

To edit with backup:

sed -i.bak '/debug/d' app.log

Be careful with delete commands. Preview first.

To print only lines that match a pattern:

sed -n '/ERROR/p' app.log

Plain meaning:

  • -n: do not print every line automatically;
  • /ERROR/p: print lines that match ERROR.

For simple searching, I usually use grep. But this is useful when I am already building a sed workflow.

Be careful with special characters

sed patterns are regular expressions. Some characters have special meaning:

. * [ ] ^ $ \ ( )

If you want to replace exact text containing these characters, you may need to escape them.

Example: a dot . in regex means “any character”. To match a real dot, use \.:

sed 's/example\.com/api.example.com/g' config.txt

This is one reason I prefer building the command slowly instead of typing from memory.

A practical example

Suppose this .env file exists:

APP_ENV=production
API_URL=https://old.example.com
PUBLIC_URL=https://old.example.com

If I only want to change API_URL, I can use:

sed '/API_URL/s#https://old.example.com#https://api.example.com#' .env

After previewing, I can edit with backup:

sed -i.bak '/API_URL/s#https://old.example.com#https://api.example.com#' .env

Now the change is targeted.

My sed checklist

Before I run a sed edit, I check:

  1. Am I previewing first without -i?
  2. Is the search pattern specific enough?
  3. Do I need g to replace every match on a line?
  4. Would a different delimiter make the command clearer?
  5. Are regex characters escaped correctly?
  6. Should I limit the replacement to matching lines?
  7. If editing in place, am I creating a backup?
  8. Am I running this on one file before running it on many files?

sed is powerful because it is fast and scriptable. The safe way to use it is to make each step visible: preview, review, then edit.

Comments

Comments are welcome — please read the comment policy first. Powered by giscus and GitHub Discussions.