# How I use sed safely for search and replace

A beginner-friendly guide to sed replacement commands, delimiters, backups, dry runs, deleting lines, and avoiding destructive edits.

- Date: 2026-09-27
- URL: https://ilham.dev/posts/how-to-edit-text-with-sed-safely/
- Markdown: https://ilham.dev/posts/how-to-edit-text-with-sed-safely/index.md
- Tags: linux, sed, text, tools
- Reading time: 4 min


`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](/tools/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:

```sh
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:

```text
old old old
```

Command:

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

Output:

```text
new old old
```

To replace every match on the line, add `g`:

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

Output:

```text
new new new
```

The `g` means global for each line.

## Use a different delimiter when replacing paths

The slash `/` is the common delimiter:

```sh
sed 's/old/new/g'
```

But if the text contains paths, slash becomes annoying:

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

A cleaner version uses another delimiter, such as `#`:

```sh
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.

```sh
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`:

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

For safer editing, create a backup:

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

This changes `config.txt` and keeps a backup as:

```text
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:

```sh
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.

```sh
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:

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

Be careful with delete commands. Preview first.

## Print only matching lines

To print only lines that match a pattern:

```sh
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:

```text
. * [ ] ^ $ \ ( )
```

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 `\.`:

```sh
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:

```text
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:

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

After previewing, I can edit with backup:

```sh
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.
