Linux Commands

Stream Editor (SED): The Basics

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:

ā€˜s/regexp/replacement/flags

 
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:

sed ā€˜s/first/moon/iā€™ file.txt > moon.txt

 
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:
    • searched in file.txt

    • moon.txt – When the word ā€œfirstā€ is replaced by the word ā€œmoonā€, it will be saved under ā€œmoon.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:

sed ā€˜3s/line/angel/iā€™ file.txt > angel.txt

 
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ā€?

sed ā€˜3,4s/line/angel/iā€™ file.txt > angel2.txt

 

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:

sed ā€˜s/line/sun/gā€™ file.txt >  sun.txt

 
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:

sed ā€˜/last/s/line/ghost/ā€™ file.txt >  ghost.txt

 
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:

sed ā€˜/last/!s/line/ghost/ā€™ file.txt >  ghost2.txt

 
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:

sed ā€˜3,4!s/line/night/iā€™ file.txt > night.txt

 
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:

sed ā€˜s/first/day/; s/second/night/; s/third/ghost/; s/last/ghost/;ā€™ file.txt > combination.txt

 

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!

About the author

Kalyani Rajalingham

I'm a linux and code lover.