Sed: That bash command is its own string manipulation library

Image for post
Image for post

The find & replace functionality already exists built-in within all programming languages. But what if you are planning your deployment pipeline or just want to apply a start up script, and lets say you need to edit some files on the fly.

This is when sed command come to the rescue. If you are using Ubuntu or any other distribution of Linux, then you will have GNU sed command by default. However, if you are using Mac OS, you will need to install a full functional sed:

$ brew install gnu-sed — default-names

Basics:

Once you make sure that sed is installed, lets start with the basics. Here is sed’s syntax:

$ sed [flags] s/{1}/{2}/{3} <file-name>

So what do we have here?

First argument contains the search and replace functionality. {1} is the search part where you can type string or regular expression that you want sed to scan a certain file in order to find. The second argument is the path to the file you want to scan. {2} is the replacement of what sed found.

By default, sed only change the first occurrence of {1} into {2}, but if it finds other {1}s it will not be changed unless you tell sed to do so. And that’s how you can use {3}. It can contain one character option where it gives more functionality to sed. For instance, if we use the letter g as {3}, it will initiate a global find and replace through the entire file.

Examples:

Enough explaining syntax, it’s boring. Lets have an example, consider the following text inside message.txt :

Dear Megan,

I received your message. I told Donna to cook a special meal for you. Donna & Mike will be waiting for you at the airport.

John

Now lets run the following sed command assuming we are in the directory where message.txt file exists:

$ sed s/Donna/Jenn/ message.txt

Dear Megan,

I received your message. I told Jenn to cook a special meal for you. Donna & Mike will be waiting for you at the airport.

John

as you can see only the first occurrence of the name Donna has been replaced with Jenn. To perform a global change you have to add g as third option.

$ sed s/Donna/Jenn/g message.txt

If you tried the commands above, you will notice that changes will be printed on the terminal but will not be applied inside the file itself. Again, you have to tell sed to do that for you by passing -i flag.

$ sed -i s/Donna/Jenn/g message.txt

Now if you checked the file it will be changed.

There are many other useful ways you can use sed. You can delete lines by passing d option instead of g and omitting {2} since it’s only find and delete. And you can use the flag -n to suppress the results from being printed on the terminal.

sed is a very powerful tool. Whether you are a developer or a DevOps engineer, you definitely are going to need it.

Lead Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store