An Introduction to Sed Stream Editor

For most people, sed is one of those tools in your proverbial *nix shed that you see but never really get around to using. The reasons vary, but usually it’s a failure to recognize the usefulness that leaves sed behind. Let’s dust off the cobwebs and start doing some basic work with this awesome utility!

Example 1:

There is a bash script you found online that is going to help you automate a process you find really annoying. However, you can’t seem to get it to run at all. After spending a few minutes investigating you pinpoint the problem to one function call on line 189. You think that even without this particular call, the script should still work fine. Let sed take a crack at it.

sed -i “189d” ./script.sh

That’s it. With the -i flag, you are replacing the original file (script.sh) with your “new” script.sh, one that has line 189 deleted. Pretty simple right?

But now it turns out that you were wrong and the script still won’t execute properly. This time you see a block that ranges from lines 243 to 784 which seems to do nothing useful besides break the script.

sed -i “243,784d” ./script.sh

Deletion isn’t the only thing we can do here, we can also add text after a line like so:

sed -i “46 a 
#Made by me” ./script.sh

Or before a line:

sed -i “46 i 
#Made by you” ./script.sh

We can even add text following a pattern as well!

sed -i ‘/bash/ a 
#Made by us” ./script.sh

Example 2:

So we want to use packer to fetch, package, and build from the AUR. The problem is that we’re trying to install a package that is out of date. Easy fix! Just edit the PKGBUILD right? Well, I hope you like vi(m), because that’s packer’s default editor.

Let’s change that, shall we? The editor we use to alter PKGBUILDs with packer is determined in this little block of code:

if proceed; then

${EDITOR:-vi} "$2"

fi

Easy fix! All we need to do is replace vi with our chosen editor. Let’s use nano for simplicity.

sed -i “s/-vi/-nano/” /usr/bin/packer

That’s it! Replace the first occurrence of -vi with -nano and we’re good to go. But for the sake of good practice, let’s make sure we backup the previous copy of packer in case we make a mistake. Small change:

sed -i.backup “s/-vi/-nano/” /usr/bin/packer

Example 3:

We have a list of emails in a file creatively named “listofemails.txt” in our system. These emails are pulled from responses to a survey the company sent out, but it looks like the software we run to do that had a bug in it. For some strange reason, the character count of a domain is limited to five. This leaves certain emails malformed like so:

hansolo@lifar.com

When we expect:

hansolo@lifars.com

Thankfully, most responses are from emails with domains that contain less than five characters and the only two providers we need to worry about are LIFARS and Hotmail.

We’ll fix the bug in the program, but those responses are long gone. We need to correct the error ourselves. With sed, this isn’t hard at all.

sed -i “s/@lifar./@lifars./g” ./listofemails.txt
sed -i “s/@hotma./@hotmail./g” ./listofemails.txt

Unlike our previous example where there was only one occurrence of -vi, there are many mistakes here. To replace all occurrences of “lifar” and “hotma”, we add ‘g’ to the end in order to make it a global.

But it’s kind of irritating to write two separate commands, let’s just do:

sed -i -e “s/@lifar./@lifars./g” -e “s/@hotma./@hotmail./g” / ./listofemails.txt

With the ‘e’ flag we can execute as many scripts as we want on a file.

As you can see, the sed stream editor can actually come in handy in many scenarios – and this is just scratching the surface. I’d recommend you to visit this tutorial website where you can learn what sed truly offers.