Jump to content

Stream Editor (SED): The Basics


Linux Hint

Recommended Posts

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:

Picture1-13.png
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:

Picture2-11.png
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”.

Picture3-11.png
What if we want to replace or transform lines “3” and “4”?

sed3,4s/line/angel/i’ file.txt > angel2.txt

 
Picture4-8.png
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”.

Picture5-6.png
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.

Picture6-5.png
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”.

Picture7-3.png
We can also do this with line numbers:

sed3,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”.

Picture8-3.png

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

 
Picture9-3.png

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!

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...