10 Real-World Sed Command Examples Every DevOps Engineer Has Lived Through

TLDR Icon Too Long; Didn't Read

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.

Scenario 1

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
Scenario 2

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
Scenario 3

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!

Scenario 4

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.

Scenario 5

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.

Scenario 6

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.

Scenario 7

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.

Scenario 8

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.

Scenario 9

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.

Scenario 10

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 or perl 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

Please follow and like us:
Prasul S
Love to know your thoughts on this:

      Leave a reply


      PixelHowl HQ
      Your ultimate playground for DevOps adventures, thrilling tech news, and super-fun tutorials. Let's build the future, together!
      Chat with us!
      connect@pixelhowl.com
      Feel free to discuss your ideas with us!
      © 2025 PixelHowl. All rights reserved. Made with ♥ by tech enthusiasts.
      PixelHowl
      Logo