SED, also known as stream editor, is a very useful tool. It is used to search for a particular word or a pattern and subsequently do something to the word or pattern or in other words transform it. In Windows, SED is also known as the āfindā and āreplaceā functions. SED comes with Ubuntu, so thereās no need to install anything; just start using it. In this tutorial, we will you how to use SED or the stream editor.
The āSā Command
The most important of all commands in SED or the stream editor is the āsā command. The āsā stands for substitute. The syntax is as follows:
So, letās use a file called āfile.txtā for the examples. Hereās what āfile.txtā looks like if you cat it:
Letās use an example to show how the āsā command works:
When such an expression is given, it means:
-
- s ā It stands for substitute.
- first – The word to search for in the file called āfile.txtā.
- moon – The word āfirstā is replaced by the word āmoonā.
- i ā It stands for ignore. Weāll ignore this part for the first bit.
- file.txt – The file where SED is going to search for the pattern or the word. In this case, the word āfirstā will be:
- moon.txt – When the word āfirstā is replaced by the word āmoonā, it will be saved under āmoon.txtā.
searched in file.txt
So, whatās happening here? SED substitutes the word āfirstā for āmoonā in only the first instance (that means that if the word āfirstā happens to occur multiple times, it wonāt replace it all over or replace it multiple times). The file it searches is called āfile.txtā and once the transformation or the replacement is made, it will be saved under āmoon.txtā.
This is what it looks like:
Please remember to put the ā/ā where it needs to be. If you omit a ā/ā, SED wonāt accept the command.
Thus far, we only replaced the word āfirstā with āencounteredā with the replacement. Now, suppose that we want to replace the word ālineā (which occurs many times – four times to be specific) in the third line with the word āangelā.
How do we specifically target that third line? We use the following command:
So, what just happened here? Well, the ā3ā specifies the line number. Therefore, it goes to the third line. Then, substitute the word ālineā for āangelā in the file called āfile.txtā and save the transformed file as āangel.txtā.
What if we want to replace or transform lines ā3ā and ā4ā?
Note that in the previous example, we used the āi” flag for ignore. Now, we use the āgā flag for global.
Letās use an example to show how the āsā command works:
When such an expression is given, it means:
The āgā stands for global. Remember that in the first example, when we use the āi” flag, there is only a single replacement. Now that we added a āgā for global, it means substitute everywhere. So, instead of saying first line, second line, third line, and last line, it says first sun, second sun, third sun, and last sun. It replaces the word line in the entire file (everywhere) with the word āsunā.
Now, what if we want to select a single line based on a word it contains? Well, we can see that the last line of āfile.txtā has the word ālastā in it. Now, suppose that we want the āThis is the last line. This is the last sentence.ā sentence to become āThis is the last ghost. This is the last sentence.ā
We write the following:
The ālastā here tells SED to look for the line that has the word ālastā and then replace the word ālineā with āghostā within that line.
Now, suppose that we want to do the opposite. Suppose that we want every line without the word ālastā to have the word ālineā changed to āghostā. Letās write the following:
As you can see here, every line except the last one (which contains the word ālastā) has the word ālineā replaced with the word āghostā.
We can also do this with line numbers:
In this case, lines 3 and 4 are omitted but every other line has the word ālineā replaced by the word ānightā.
Multiple Commands
Now, what if you had multiple commands? Would you rather do it one at a time or all at once and save yourself some time and work?
What if we want to change the word āfirstā to ādayā, āsecondā to ānightā, and āthe restā to āghostā? We use the semi-colon to do this. Donāt forget to put the semi-colon at the end!
Please note that you donāt absolutely have to put the āi” flag or the āignoreā flag but you absolutely have to put the slash (/) after the transformation phrase.
Now, letās check it out with an example:
Conclusion
The stream editor or the SED is a way of selecting a word or a pattern and transforming it. Itās actually the command-line equivalent of the Windowās āfindā and āreplaceā functions. The SED command can get really complicated but if you at least know the basics, youāre ready to take it on! SED is actually a very powerful tool with many functions. Though we canāt cover them all in one tutorial, we covered the basics of SED. In essence, we learned how to transform a particular word using the āsā command where āsā stands for substitute. We can substitute the words for other words, selectively choose a line where the substitution will occur, or even negate it. Either way, this is the easiest part about SED.
Happy Coding!