Linux Commands

SED Features and Examples

SED stands for stream editor and it is a command in Linux that is used to perform different operations on files such as text replacement, insertion, text search, and etc. This command is extremely easy to use and can do wonders if used properly. Therefore, in this article, we will talk about some of the features of the SED command followed by a few relevant examples so that you will be able to learn its usage.

Features of the SED Command:

Some of the most important features of the SED command are mentioned below:

  • This command is used for modifying the files without opening them which makes the overall process much quicker.
  • It also allows you to make modifications by using different regular expressions with which you can conveniently search for different patterns.
  • It can also be used for modifying strings within the terminal without using any file.

Another point to be mentioned over here is that the SED command is case-sensitive. Whichever word you wish to change or replace with this command, you will have to write it in the exact cases, otherwise, it will not work properly.

Examples of the SED Command:

To use the SED command in Linux, you will have to go through the following four examples. Before proceeding with these examples, we would like to share with you the text file that we will be using in all of these examples. For displaying the contents of this file, we will use the “cat” command in the manner shown below:

$ cat file.txt

The contents of this sample text file are shown in the following image:

Example # 1: Replacing the First Occurrence of a Word in Every Line of the File

In this example, we will be using the SED command for replacing the first occurrence of a word in every line of our sample text file. For that, we have used the SED command in the manner shown below:

$ sed ‘s/Hello/Hi/’ file.txt

In this command, we have used the “s” operator for specifying the substitution operation that is going to take place. “Hello” is the word that we want to replace whereas “Hi” is the word that we want to replace it with. “file.txt” is the name of the file in which this operation will take place.

As soon as you will run this command, its output will immediately appear on the terminal as shown in the following image. You can conveniently verify that this command has successfully replaced the first occurrence of the specified word in all the lines of our target text file.

Example # 2: Replacing the nth Occurrence of a Word in Every Line of the File

In this example, we will use the SED command for replacing the nth occurrence of a word in every line of our sample text file. For that, we will use the SED command as shown below:

$ sed ‘s/Hello/Hi/2’ file.txt

Through this command, we want to substitute the second occurrence of the word “Hello” with the word “Hi” in every line of our file. You can replace “2” with any other number depending on the position of occurrence of the specified word where you wish to make the substitution.

The following output shows that the second occurrence of our specified word in every line of our file has been replaced successfully:

Example # 3: Replacing all the Occurrences of a Word in the File

Now, we want to replace all the occurrences of a word in our sample text file at once. For that, we are going to use the SED command in the manner shown below:

$ sed ‘s/Hello/Hi/g’ file.txt

Here, we have used the “g” flag of the SED command for making a global replacement of the specified word: all the occurrences of this word will be substituted at once.

The output of this command is shown in the following image from where you can confirm that all the occurrences of the specified word have been replaced successfully.

Example # 4: Replacing the First Occurrence of a Word in a Specific Line of a File

Finally, in this example, we will use the SED command for replacing the first occurrence of the specified word only within the specified line. For that, we will use the SED command in the manner shown below:

$ sed ‘2 s/Hello/Hi/’ file.txt

Through this command, we want to replace the first occurrence of the specified word only in the second line of our sample text file because of which we have used the number “2”. You can replace it with any other number of your choice depending on the line number of the line where you wish to make the replacement.

You can witness from the following output that the first occurrence of our specified word has been replaced successfully in the second line of our sample text file.

Conclusion

In this tutorial, we wanted to discuss the features of the SED command in Linux along with a few useful examples to elaborate on its usage. For that, we briefly introduced you to the working of this command after which we talked about some of its noteworthy features. Finally, we discussed four different examples with you that demonstrated the usage of this command. By going through these examples, you will quickly be able to learn the usage of the SED command in Linux.

Similar Posts