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.
Here at LinuxAPT, we shall look into a few features of the SED command followed by a few relevant examples so that you will be able to learn its usage.
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:
$ cat file.txt
Here, we will be using the SED command for replacing the first occurrence of a word in every line of our sample text file. To achieve this, we have used the SED command:
$ sed 's/Hello/Hi/' file.txt
You can see that 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.
Once you run this command, its output will immediately appear on the terminal. 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.
Here, we will use the SED command for replacing the nth occurrence of a word in every line of our sample text file. Now, use the SED command:
$ sed 's/Hello/Hi/2' file.txt
With 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 output will show 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:
$ 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 will confirm that all the occurrences of the specified word have been replaced successfully.
Here, 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:
$ sed ‘2 s/Hello/Hi/’ file.txt
With 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 output that the first occurrence of our specified word has been replaced successfully in the second line of our sample text file.
This article covers the concept of the SED command in Linux along with a few useful examples to elaborate on its usage. In fact, SED is a text stream editor used on Unix systems to edit files quickly and efficiently. The tool searches through, replaces, adds, and deletes lines in a text file without opening the file in a text editor.
The main syntax for using the Linux sed command is:
$ sed OPTIONS... [SCRIPT] [INPUTFILE...]
You can execute sed with the following command-line options: