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.
Key Features of the SED Command includes:
- Great for modifying the files without opening them which makes the overall process much quicker.
- Allows you to make modifications by using different regular expressions with which you can conveniently search for different patterns.
- Used for modifying strings within the terminal without using any file.
- 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 using 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:
$ cat file.txt
1. Replace the First Occurrence of a Word in Every Line of the File
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.
2. Replace the nth Occurrence of a Word in Every Line of the 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.
3. Replace 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:
$ 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.
4. Replace the First Occurrence of a Word in a Specific Line of a File
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.
[Need help in fixing Linux system issues ? We can help you. ]