How to use Sed command in Linux to update files

Posted on November 7th, 2019

As a system administrator, you often have to update configuration files. It is not a problem if you have to open a file, update the content and save it every week or once in a month. But in case you have to update many configuration files regularly, the traditional method to open, update and save a file is not effective. That is why, we have sed command in Linux.

Sed command can solve this issue very effectively. We can use sed command to manipulate files easily. Not only manipulation, but you can also extract specific parts of the file to read. Unlike head and tail commands, you can get specific parts of the file to read based on line numbers.

In this guide, we will learn all the important features of sed. First of all, we will learn how to view file contents using sed command. Then, we will move on to manipulation part. So, Let’s get started.

Syntax of Sed command in linux

Before we learn how to use it practically, We are going to see the syntax of the command. It is very simple, like other commands in linux, you will see a command with options and input file. But sed has one more part, and that is script. So, Here is how the syntax of Sed command looks like.

$ sed [OPTIONS...] [SCRIPT] [INPUT_FILE]

There are three parts in the command. Options allow you to specify different conditions to run a command. Script can be a one-liner that you can write directly into the command. Script can also be a file that you can include in your command using -f  or --file= option. And finally, a input file, a file on which you want to perform actions.

View file using Sed

Sed command allows you to view file contents. It will also allow you to view file contents with updated content. Before we start manipulating a file, let us see How to view file contents using sed.

View range of lines using Sed

Sed allows you to read specific set of lines from a specific file. For example, Let’s say we have a file called example.txt and we want to view 5-10 lines from that file. To do so, we have to execute the following command.

$ sed -n '5,10p' /path/to/example.txt

This command will show us the 5th, 6th, 7th, 8th, 9th, and 10th line of the file.

Exclude range of lines using Sed

Suppose, you want to view the whole file except specific range of lines. For example, Let’s say we want to view the whole example.txt file, except 5-10 lines. In this case, we have to execute the following command.

$ sed '5,10d' /path/to/example.txt

The output will contain everything from the file except 5th, 6th, 7th, 8th, 9th and 10th line.

Exclude lines starting with specific word/character

Let’s assume, our example.txt file is a configuration file containing comments starting with # and we want to view the configuration file but not the comments. In that case, we have to execute the following command.

$ sed '/^#/d' /path/to/example.txt

You can also remove ^ from our command to exclude lines containing a word or character anywhere in the line.

View lines containing specific word

grep command in linux is a really great tool for this task. However, You can also do this using sed command. For instance, Let’s say we want to view lines starting with Interserver in our example.txt file. In this case, we have to execute the following command.

$ sed -n '/^Interserver/p' /path/to/example.txt

Just like the previous example, you can remove ^ from the command to view all the lines containing the word linux no matter the position. Now, Let us see how to update our file using sed command.

Update File using Sed

Sed command is mostly used to update files without opening them in edit mode. To be more specific, replacing words and characters in a file without opening a file in edit mode.

Note: The commands we are going to execute will only display a file with updated content. However, If you want to actually update a file, use -i option along with the sed command.

First of all, let us see how to replace the nth occurrence of a word in a line.

Replace nth occurrence of word in a line

The command we are going to execute will update the first occurrence of a word or a character in a line. If you have multiple lines in a file containing that specific word or character, it will update the first occurrence in every line that matches.

$ sed 's/old_word/new_word/' /path/to/file.txt

If you want to update the second occurrence of a word/character in a file, execute the following command.

$ sed 's/old_word/new_word/2' /path/to/file.txt

Similarly, you can update the nth occurrence of a word in a line. Now, Let us see what we can do to update every match in a file.

Replace word or character globally

If you want to replace every apple with orange in a file, you can execute the sed command like this.

$ sed 's/apple/orange/g' /path/to/file.txt

It will match every word in a file. And it will replace every apple word with the orange word in a file regardless of the location of the word.

Replace word or character in given range

Now, If you want to replace every apple with orange in a specific file between 10th and 20th line, execute the following command.

$ sed '10,20 s/apple/orange/g' /path/to/file.txt

This command will replace every apple with orange in the range of 10th to 20th line of the file.

Replace multiple words or characters at once

If you want to perform multiple substitutions at once, you can separate substitutions using ;operator. Let’s say, we want to replace every apple with orange as well as every watermelon to mango. To do this, we have to execute the following command.

$ sed 's/apple/orange/g;s/watermelon/mango/g' /path/to/file.txt

Or, if this command looks more complex than it should be, you can use -e option instead. With the -e option, the above given command will look like this.

$ sed -e 's/apple/orange/g' -e 's/watermelon/mango/g' /path/to/file.txt

So, this is how you can replace multiple words or characters at once using sed command in linux.

 

Conclusion: Sed is a very powerful command in linux that allows you to update configuration files with ease. With proper practice, you can efficiently manage multiple configuration files on the server very fast. You can run man sed to learn more about options available in sed command that will allow you to do more with less. There are many useful commands in sed like -f that allows you to run your sed script file.

If you have any questions regarding this guide on Sed, please use the comment section given below. We will respond as soon as possible with the answer.

Leave a Reply