Replace text from a file with sed from the command line

In a new project I noticed that a static website building tool was wrongly generating the sitemap.xml file, the website sitemap, with http instead of https in the pages url. I was in a hurry to deploy it so I had to make a quick hack to fix the sitemap.xml file straight from the command line. I used sed.

When you need to replace some text in a file, and you don’t want to do it manually, using sed is a good approach. Here I’m going to show you how to use sed to replace text from a text file.

What is sed

The sed is text editor that, unlike a normal interactive text editor, edits a stream of data. In a normal interactive text editor, such as vim or nano, you interactively edit a text files using the keyboard to enter commands. Being a stream editor , sed edits a stream of data based on a set of rules you supply before the editor processes the data.

A stream editor like sed is a perfect match for scripting and automating text manipulation. It runs in a shell command line being very useful for quick one time text manipulations as well as to be used in scripts that will be run multiple times.

The editor sed is available for Linux and Mac Os.

How it works

The sed editor manipulates data in a data stream (a file or input from a pipeline) based on commands you either provide in the command line or in a command text file. The working flow of the sed is like the following:

  1. Reads a line at a time from the input stream (file or pipeline).
  2. Matches the read data with the commands provided in the command line or command script file.
  3. Manipulates the data as specified by the commands.
  4. Outputs the new data to standard output or to where you redirected it.
  5. Reads another line and repeats the steps from point 2 onward.
  6. When all data is read and processed it terminates.

How to use it

Typically you invoke sed like this:

sed OPTIONS SCRIPT INPUTFILE

For example, let’s assume you have a sitemap.xml file where the locations are with http and you wanted to be with https. You could use the command line bellow to create a new sitemap file where the <loc>http: text is replaced by <loc>https: for all occurrences.

sed 's/<loc>http:/<loc>https:/' sitemap.xml > new_sitemap.xml

Taking the same use case example, if you did not want to create a new file but instead modify the input file then the command would be the following.

sed -i 's/<loc>http:/<loc>https:/' sitemap.xml

Instead of files you can use a pipeline as the input, like the following.

sed "This is my small example" | sed 's/example/pipeline example'

This command will output the following text: This is may small pipeline example

There’s much more you can do with sed and we recommend you look into the resources linked in the following section.

Further reading

0%