Linux command list

sed command: replace text without opening the file

__ __ _ _____
\ \ / /__| |_|_ _|__ _ __ _ __ ___
\ \ /\ / / _ \ '_ \| |/ _ \ '__| '_ ` _ \
\ V V / __/ |_) | | __/ | | | | | |
\_/\_/ \___|_.__/|_|\___|_| |_| |_| |_
 
A sandbox for trying sed. Nothing here can touch your real files.
user@webterm:~/project$
 

sed substitutes text in a file without opening it. Learn the `s/old/new/` form, how `-i` edits the file itself, and how to select or delete lines, by running the commands in a real terminal in your browser.

Updated: 2026-09-06

Syntax

sed [options] 'script' file...

The script you will use most is s/old/new/, a substitution.

Try it first

Look at what you are about to change.

$ cat config.yml
port: 3000
host: localhost
debug: false

Replace localhost.

$ sed 's/localhost/example.com/' config.yml
port: 3000
host: example.com
debug: false

There is the result. The file, however, has not changed.

$ cat config.yml
port: 3000
host: localhost
debug: false

By default sed only prints. That is what makes it safe to try against a production config before committing to anything.

Changing the file itself

Once you have seen the result, add -i.

$ sed -i 's/localhost/example.com/' config.yml
$ cat config.yml
port: 3000
host: example.com
debug: false

-i is short for in place. Add a suffix to keep a copy of what was there before.

$ sed -i.bak 's/3000/8080/' config.yml
$ ls
config.yml  config.yml.bak  logs/  urls.txt
$ cat config.yml.bak
port: 3000
host: example.com
debug: false

config.yml.bak is the copy. Make it a habit on servers and you always have a way back.

Two matches on one line

By default only the first match on each line is replaced.

$ cat urls.txt
api=http://example.com cdn=http://cdn.example.com
$ sed 's/http:/https:/' urls.txt
api=https://example.com cdn=http://cdn.example.com

The second one survived. g at the end replaces all of them.

$ sed 's/http:/https:/g' urls.txt
api=https://example.com cdn=https://cdn.example.com

g stands for global.

Selecting and dropping lines

Besides substituting, sed can pick lines.

$ sed '/DEBUG/d' logs/app.log
INFO  server started
ERROR connection refused
INFO  retrying
ERROR timeout

/DEBUG/d means delete every line containing DEBUG. Line numbers work too.

$ sed -n '2p' logs/app.log
DEBUG cache warmed
$ sed -n '1,3p' logs/app.log
INFO  server started
DEBUG cache warmed
ERROR connection refused

-n turns off the default printing, and p names the lines to print.

When you actually reach for it

sed is what you type for the same edit applied mechanically.

SituationWhat to type
Swap a value in a config filesed -i.bak 's/3000/8080/' config.yml
Move every URL to httpssed -i 's/http:/https:/g' urls.txt
Read a log without the noisesed '/DEBUG/d' app.log
Pull out a range of linessed -n '10,20p' app.log
Comment out every linesed 's/^/# /' config.yml

^ matches the start of a line and $ the end.

$ sed 's/^/# /' config.yml
# port: 8080
# host: example.com
# debug: false

Things that trip people up

-i cannot be undone. The file changes the moment you run it. Either look at the result without -i first, or use -i.bak so there is a copy.

Change the separator when the text contains /. Replacing a path means the separators and the content are both slashes, which nobody can read. The separator does not have to be /, so use # or |.

$ echo /home/user/project | sed 's#/home/user#/srv#'
/srv/project

It works in a pipe.

$ grep ERROR logs/app.log | sed 's/ERROR/failure/'
failure connection refused
failure timeout

A missing file says so.

$ sed 's/nope/x/' nothing.txt
sed: can't read nothing.txt: No such file or directory

Practise it hands-on

webterm.appthis site

learn.webterm.appa separate site

>_WEBTERM LEARN

WebTerm Learn: from one command to actually using it

Commands stick when they show up in a real sequence of work, not one at a time. There is a course that builds them up in order.

See the course

Related commands

grep / awk / tr / vim

Frequently asked questions

I ran sed but the file did not change.
By default sed prints the result and leaves the file alone. Add `-i` to change the file itself. Running it without `-i` first, to see what would happen, is the safe order.
What is the `g` at the end of `s/a/b/`?
It replaces every match on the line. Without it, only the first match on each line is replaced.
Can I keep the original?
`sed -i.bak 's/old/new/' file` leaves the previous contents in `file.bak`. Worth doing whenever you touch a config file on a server.
Can it pick out or drop particular lines?
`sed -n '2p'` prints only line 2 and `sed '/DEBUG/d'` drops lines containing DEBUG. `-n` turns off the default printing and `p` says which lines to print.
When would I use this instead of vim?
sed for the same edit repeated mechanically, vim for edits you want to watch. sed never opens the file, which is why it belongs in scripts and across many files.