
I still remember the night sed
saved me from a two-hour editing nightmare.
It was a Friday, the kind where deployments were supposed to go smoothly, but somehow a tiny config change slipped into production. We had thousands of Nginx configs pointing to the wrong backend. Manually fixing them was out of the question – sed
came to the rescue in less than a minute.

The Great Backend Migration
It was Friday night when configs across hundreds of servers still pointed to old-server
. Panic rose; editing each file would take hours. With one precise sed
command, the engineer swapped every reference in seconds. What could have been chaos turned into relief, proof of automation’s power.
Problem: After switching from old-server
to new-server
, hundreds of configs were still pointing to the old host.
Why it matters: This avoided hours of manual edits and got production back on track.
sed -i 's/old-server/new-server/g' /etc/nginx/sites-available/*.conf
Watch out for: If your replacement contains slashes (/
), change the delimiter:
sed -i 's|/var/www/html|/srv/webroot|g' file.conf

Cleaning Messy Logs Before Parsing
The monitoring script kept failing, but the logs looked fine at first glance. Hidden among the entries were dozens of sneaky blank lines. Instead of scrolling endlessly, the engineer used a single sed
command to sweep them away. Clean logs, smooth parsing, and a quick win before coffee.
Problem: We could see hundreds of configuration that did have empty lines showing up on the configuration and for this we did use sed
Why it matters: As usual this did avoid manually going through each file
sed -i '/^$/d' logfile.log
Watch out for: In case there are slash characters or similar on these entries then you can tweak it using
sed -i '/^[[:space:]]*$/d' logfile.log

Comment Out Matching Lines
The team was testing a new app on the staging server when a port conflict stopped everything cold. Deleting the line in Apache’s config felt risky, what if they needed it back tomorrow? Instead, the engineer simply “shushed” it with a sed
command, adding a harmless #
in front. The port went quiet, the conflict vanished, and the line stayed safe for later. It felt less like ripping a page out of the book and more like putting in a gentle bookmark.
sed -i '/Listen 80/s/^/#/' /etc/httpd/conf/httpd.conf
The command line is simple as it does add a # symbol before the entry ‘Listen 80’ entry on httpd.conf.
If you are trying this then please ensure that you are as a user with sufficient permissions!

Uncomment Specific Lines
A week after disabling HTTPS for troubleshooting, the engineer returned to find the site working fine but still running insecurely. Instead of scrolling through configs and manually deleting #
marks, they ran a simple sed
command. With a tiny nudge, the line woke back up, enabling secure traffic again. It felt like flipping the switch back on, without hunting in the dark.
sed -i '/#Listen 443/s/^#//' /etc/httpd/conf/httpd.conf
The command is easy to understand, it searches for #Listen 443
and removes the leading #
.
Enabling the port 443 for the server.

Adding a Setting in the Right Place
During an Nginx deployment, things broke because a key setting wasn’t there. Instead of just tacking it on at the bottom, the engineer carefully placed it before the right directive with sed
. That small precision brought order back, proving that where you add a setting matters as much as the setting itself.
sed -i '/worker_processes/i worker_rlimit_nofile 65535;' /etc/nginx/nginx.conf
That “i” flag means insert before match. Feels surgical and it is.

Dropping Notes for Future You
Late one night, the engineer raised the max_connections
value to handle extra load. But knowing that tomorrow’s self might forget why, they added a note right after the setting using sed
. That small comment became a lifesaver later, reminding them of the reason behind the change. Future-you always appreciates those breadcrumbs.
sed -i '/max_connections/a # Increased for load testing' my.cnf
That “a” flag appends after the match, making it perfect for comments.

Changing Only a Specific Line
I’ve been in situations where replacing a setting everywhere would break things. For example, only the 5th line needed updating:
sed -i '5s/oldtext/newtext/' file.txt
During a production rollout, only the fifth line in a config file needed correction.
A full replace would’ve broken everything else. With sed
, the engineer targeted just that single line and made the change. It felt like fixing a loose tile in a floor precise, neat, and without disturbing the rest.

Print Matching Lines Without Changing Files
Before making changes, the engineer wanted to see exactly where the word “error” appeared in a huge log. Instead of risking edits, they used sed
to simply print those matching lines. It felt like turning on a flashlight in a dark room; showing only what mattered without moving anything around.
sed -n '/error/p' logfile.log
The -n
flag hides everything except matching lines. Trust me! this habit will save you.

Scrubbing Sensitive Data
Before sending a log to a vendor, the engineer noticed entries revealing passwords. Sharing it as-is was out of the question. With a single sed
command, every line containing “password” vanished. The log stayed useful, and sensitive details stayed private. It felt like sweeping away secrets before opening the door.
sed -i '/password/d' logfile.log
One line, zero leaks.
I make sure passwords never leave my screen.

Hitting Multiple Targets at Once
During a migration, configs referenced both dev.example.com
and staging.example.com
. Instead of fixing them one by one, the engineer used sed
with extended regex to replace both in a single shot.
One command, multiple fixes; like hitting two targets with a single arrow.
sed -E -i 's/(dev|staging)\.example\.com/prod.example.com/g' config.yaml
The -E
flag enables extended regex; great for catching patterns without multiple runs.
These are the few repetitive instances, which can be useful for you guys as well.
Best Practices I’ve Learned the Hard Way
- For very complex patterns, consider
awk
orperl
instead.
- Always run without
-i
first to preview changes.
- Keep backups —
sed -i.bak
is your friend.
- Use different delimiters when your pattern has slashes.
Try out our sed command generator tool for a quick setup