How-to

The sed Command for Server Admins

sed edits text without opening an editor, which makes it ideal for scripts and quick changes to config files on many machines.

What does sed do?

sed, the stream editor, reads input line by line, applies editing commands and writes the result. By default it prints to standard output and leaves the file untouched, so you can preview any change safely. With -i, it edits the file in place. It ships with every image we install: GNU sed on the Linux images and BSD sed on FreeBSD 14.

How do I replace text?

sed 's/worker_connections 768/worker_connections 4096/' /etc/nginx/nginx.conf
sed 's/http:/https:/g' links.txt

The s command takes a pattern, a replacement and flags. Without g, only the first match per line changes. Other flags: I for case-insensitive in GNU sed, a number such as 2 to change only the second match, and p to print changed lines.

How do I edit a file in place safely?

sed -i.orig 's/^#\?MaxAuthTries.*/MaxAuthTries 3/' /etc/ssh/sshd_config.d/30-limits.conf
diff /etc/ssh/sshd_config.d/30-limits.conf.orig /etc/ssh/sshd_config.d/30-limits.conf

-i.orig saves the original with that suffix before writing. Check the result with diff, then validate the service's configuration, here with sshd -t, before reloading. On FreeBSD, -i requires an argument, so write sed -i '' 's/a/b/' file for no backup.

How do I target specific lines?

AddressApplies to
12Line 12
5,20Lines 5 to 20
$The last line
/^server {/,/^}/From a matching line to the next match
/^#/!Lines not matching
0~4Every fourth line (GNU)
sed -n '120,140p' /var/log/app/worker.log
sed '/^\[mysqld\]/,/^\[/ s/^max_connections.*/max_connections = 600/' /etc/my.cnf

The second command changes max_connections only inside the [mysqld] section, leaving other sections alone. -n with p prints only the selected lines, a quick way to view part of a large file.

How do regex groups work?

Use -E for extended regex, where parentheses group without backslashes, and refer to groups with \1 to \9:

sed -E 's/^([a-z_]+)=(.*)$/\1="\2"/' app.env
echo 'deploy 2026-12-03' | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3.\2.\1/'

& inserts the whole match. If the pattern or replacement contains slashes, use another delimiter: sed 's#/var/www/old#/srv/site#g'.

How do I insert or append lines?

sed -i '/^\[Service\]/a MemoryMax=2G' /etc/systemd/system/report.service
sed -i '1i # Managed by deploy script, do not edit by hand' /etc/cron.d/site-tasks
sed -i '$a export EDITOR=vim' /home/ana/.bashrc

a appends after the addressed line, i inserts before it. GNU sed accepts the text on the same line as shown; BSD sed needs a backslash and a newline after the command.

How do I run several commands?

sed -e 's/foo/bar/g' -e '/^$/d' input.txt
sed -f /usr/local/share/cleanup.sed input.txt

Several -e options run in order on each line; a script file with -f holds longer edits, one command per line. Deleting lines in all its forms is covered in Delete lines with sed.

When is sed the wrong tool?

For structured formats such as JSON or YAML, a parser is safer: jq for JSON and yq for YAML understand nesting that line-based edits can break. For interactive editing with confirmation, Vim's substitute is friendlier; see Find and replace in Vim. For searching rather than changing, use The grep command.

How do I test a sed command safely?

Run it without -i and compare: sed 's/old/new/' file | diff file - shows exactly which lines would change. Once the diff looks right, add -i.orig and run it for real. Testing against a copy in /tmp works too, and costs nothing.

How do I use sed on many servers at once?

Run the same command over SSH: for h in chi-web ams-web sgp-web; do ssh "$h" "sudo sed -i.orig 's/^pm.max_children.*/pm.max_children = 32/' /etc/php/8.3/fpm/pool.d/www.conf"; done. Host aliases from your SSH config keep the loop short. For more than a handful of machines, a configuration tool such as Ansible, whose lineinfile module does the same job with checks, is safer.

Keep Reading

Delete lines with sed

Delete lines with sed by number, range, pattern, blank or comment, first or last, and between markers, with in-place edits and backups.

Open

Find and replace in Vim

Search and substitute in Vim: the :s command, ranges, flags, whole words, regex groups, visual selections and changes across many files.

Open

The grep command

grep from basics to power use: case, whole words, line numbers, context, recursion, regex flavors, only-matching output and speed tips.

Open

Harden SSH on Linux

A practical hardening list for sshd on a fresh dedicated box: keys, users, rate limits, firewall scope and an outside audit.

Open

FAQ

Quick Answers

Run sed -i 's/old/new/g' file; add a suffix like -i.orig to keep a backup.

BSD sed needs an argument after -i. Use sed -i '' for no backup.

Use -n with an address and p, such as sed -n '10,20p' file.

Add -E, group with parentheses and refer to groups as \1 to \9 in the replacement.

Use the a command, such as sed '/pattern/a new line' file.