sed: Stream Editor
sed (stream editor) reads text line by line, applies editing commands to each line and prints the result. It's the standard tool for search-and-replace in files and pipelines, for deleting or printing selected lines, and for editing config files from scripts.
Substitution: s/old/new/
The command you'll use 90% of the time is s/PATTERN/REPLACEMENT/FLAGS:
echo "hello world, hello sed" | sed 's/hello/goodbye/'
echo "hello world, hello sed" | sed 's/hello/goodbye/g'
echo "Error: disk FULL" | sed 's/full/almost full/I'
echo "a-b-c-d" | sed 's/-/+/2'goodbye world, hello sed
goodbye world, goodbye sed
Error: disk almost full
a-b+c-d
| Flag | Effect |
|---|---|
| (none) | Replace the first match on each line |
g |
Replace all matches on the line |
2 |
Replace only the 2nd match |
I |
Ignore case (GNU) |
p |
Print the line if a replacement happened (with -n) |
sed doesn't change the input file unless you ask: it prints the edited text.
Working on files
cat > hosts.txt <<'TXT'
web1 10.0.0.11 staging
web2 10.0.0.12 staging
db1 10.0.0.21 staging
TXT
sed 's/staging/production/' hosts.txt
echo "--- the file itself is unchanged:"
head -n 1 hosts.txt
sed -i 's/staging/production/' hosts.txt
echo "--- after sed -i:"
cat hosts.txtweb1 10.0.0.11 production
web2 10.0.0.12 production
db1 10.0.0.21 production
--- the file itself is unchanged:
web1 10.0.0.11 staging
--- after sed -i:
web1 10.0.0.11 production
web2 10.0.0.12 production
db1 10.0.0.21 production
-i edits in place. -i.bak keeps a backup copy with the given suffix first, which is a good habit with important files. (On macOS, BSD sed requires an argument: sed -i '' ....)
Choosing lines: addresses
A command can be limited to certain lines with an address in front of it: a line number, a range, $ (last line) or a /regex/:
seq 1 8 > nums.txt
sed -n '3p' nums.txt | tr '\n' ' '; echo
sed -n '2,4p' nums.txt | tr '\n' ' '; echo
sed -n '$p' nums.txt
sed '1d;$d' nums.txt | tr '\n' ' '; echo
sed -n '1~3p' nums.txt | tr '\n' ' '; echo3
2 3 4
8
2 3 4 5 6 7
1 4 7
-nturns off automatic printing, so only lines youprint appear.ddeletes lines,;separates commands.1~3(GNU) means "every 3rd line starting at 1".
Regex addresses select lines by content, which is perfect for config files and logs:
cat > nginx.conf <<'CONF'
# main settings
worker_processes 4;
# events block
events {
worker_connections 1024;
}
CONF
sed '/^#/d; /^$/d' nginx.conf
echo "---"
sed -n '/events {/,/}/p' nginx.conf
echo "---"
sed '/worker_connections/s/1024/4096/' nginx.conf | grep connectionsworker_processes 4;
events {
worker_connections 1024;
}
---
events {
worker_connections 1024;
}
---
worker_connections 4096;
The first command strips comments and blank lines (a quick way to see the real settings in a long config). The range /start/,/end/ prints a whole block. /pattern/s/a/b/ substitutes only on matching lines.
Capture groups and special characters
In the replacement, & stands for the whole match and \1, \2 for parts captured with \(...\). With -E (extended regex) you write the groups as (...) without backslashes:
echo "2026-03-15" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3.\2.\1/'
echo "price: 42" | sed 's/[0-9][0-9]*/[&]/'
echo "Lovelace, Ada" | sed -E 's/(\w+), (\w+)/\2 \1/'
echo "/usr/local/bin" | sed 's|/usr/local|/opt|'15.03.2026
price: [42]
Ada Lovelace
/opt/bin
When the pattern contains / (paths, URLs), use another delimiter such as | or #, so you don't have to escape every slash.
Insert, append, change
| Command | Effect |
|---|---|
i\ text or i text |
Insert a line before |
a text |
Append a line after |
c text |
Replace the whole line |
y/abc/xyz/ |
Translate characters (like tr) |
printf 'name=shop\nport=80\n' > app.ini
sed '1i [server]' app.ini
echo "---"
sed '/^port/a timeout=30' app.ini
echo "---"
sed '/^port/c port=8080' app.ini[server]
name=shop
port=80
---
name=shop
port=80
timeout=30
---
name=shop
port=8080
Common mistakes
- Using double quotes carelessly.
sed "s/$old/$new/"works and lets variables in, but$and\then need extra care. Prefer single quotes unless you need a variable. - Special characters in the pattern.
.means "any character".sed 's/1.5/2.0/'also changes105. Escape it:1\.5. sed -ion a symlink replaces the link with a regular file; use--follow-symlinksif needed.
Test yourself
What does echo aaa | sed 's/a/b/' print?
- bbb
- baa
- aab
- b
Which command prints only lines 5 to 10 of file?
- sed '5,10p' file
- sed -n '5,10p' file
- sed '5,10d' file
- sed -i '5,10' file
What does sed -i 's/http:/https:/g' site.conf do?
- Prints the file with replacements
- Changes the file itself
- Creates site.conf.bak
- Only checks the syntax