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.
Some of the most important features of the SED command are mentioned below:
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.
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:
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.
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:
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.
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.
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.
Photo by Jeton Bajrami on Unsplash Date: December 4-5th, 2024 Location: Geneva, Switzerland In just…
Who will win the race to the perfect SDV? The automotive industry is experiencing a…
Software developers spend a huge amount of effort working on optimization – extracting more speed…
Welcome to the Ubuntu Weekly Newsletter, Issue 866 for the week of November 10 –…
Debian and Ubuntu are two popular Linux distributions. In this deep dive we will guide…
In this article, we will see how to Install Google Cloud BigQuery Python client library…