Home > Article > Computer Tutorials > Stream Editor (SED): Basics
SED, also known as the Stream Editor, is a very useful tool. It is used to search for a specific word or pattern and then perform some operation on that word or pattern, or in other words, transform it. In Windows, SED is also known as the Find and Replace function. SED comes natively with Ubuntu, so there's nothing to install; just start using it. In this tutorial we will show you how to use SED or Stream Editor.
The most important command in SED or stream editor is the "s" command. The "s" stands for substitute. The syntax is as follows:
/regexp/replacement/flags
So, let's use a file called "file.txt" as an example. Here's what "file.txt" looks like if you trace it:
Let us use an example to illustrate how the "S" command works:
SED‘S/First/Moon/I’file.txt>moon.txt
When an expression like this is given, it means:
Searched in file .txt
In SED, the word "first" is replaced with "moon" only in the first instance (this means that if "first" appears multiple times, only the first time will be replaced). This command searches in a file named "file.txt" and once the replacement is complete, the results will be saved to the "moon.txt" file.
This is what it looks like:
Please remember to put "/" where needed. If "/" is omitted, SED will not accept the command.
So far, we have only replaced "first time" with the word "encounter". Now, let's say we want to replace the word "line" (which appears multiple times - four times, to be specific) in the third line with the word "Angel".
How do we specifically target the third line? We use the following command:
SED‘3S/Line/Angel/I’file.txt>angel.txt
So, what just happened here? Okay, "3" specifies the line number. So it comes to the third line. Then, replace "angel.txt" with "line" in the file named "file.txt" and save the converted file as "angel.txt".
What if we want to replace or convert rows "3" and "4"?
Sed‘3,4s/line/Angel/I’file.txt>angel2.txt
Note that in the previous example, we used the "i" flag to represent ignore. Now, we use the "g" flag to represent global.
Let us use an example to illustrate how the "s" command works:
sed’s/line/sun/g ‘file.txt> sun. txt
When an expression like this is given, it means:
G stands for global. Remember, in the first example, when we used the "i" flag, there was only one substitution. Since we added a "g" to global, this means there are alternatives everywhere. So, instead of saying first row, second row, third row and last row, it says first sun, second sun, third sun and last sun. It replaces lines of words (everywhere) in the entire file with the word "sun".
Now, what if we want to select a line based on the words it contains? Well, we can see that the last line of "file.txt" has the word "last". Now, let's say we want "This is the last line". This is the last word." "This is the last ghost. This is the last sentence."
We write as follows:
Sed‘/last/S/line/host/’file.txt>.ghost.txt
The "last" here tells SED to find the line containing the word "last" and then replace the word "line" with "ghost" within that line.
Now, let's say we want to do the opposite. Suppose we want every line without the word "last" to have the word "line" changed to "host". Let's write the following:
Please select ‘/last/! S/line/ghost/’file.txt>.ghost 2.txt
As you can see here, every line except the last line (which contains the word "last") has the word "ghost" replaced with the word "line".
We can also use line numbers to achieve this:
Sed‘3,4! S/行/夜/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".
So, what if you have multiple commands? Would you rather do them one at a time or all at once to save yourself time and work?
What should we do if we want to change the word "first" to "day", the word "second" to "night", and the word "rest" to "ghost"? We do this using semicolons. Don't forget to put the semicolon at the end!
Please note that you do not have to put the "i" flag or the "ignore" flag, but you must put the slash (/) after the conversion phrase.
Now, let us verify it with an example:
sed’s/first/day/;s/second/night/;s/third/ghost/;s/last/ghost/;’file.txt> combination.txt
The Stream Editor, or SED, is a method of selecting words or patterns and converting them. It is effectively the command line equivalent of Windows' "find" and "replace" functions. SED commands can get very complex, but if you at least know the basics, you're ready! SED is actually a very powerful tool. While we can't cover them all in one tutorial, we cover the basics of SED. Essentially, we learned how to convert a specific word using the "s" command, where "s" stands for replacement word. We can replace words with other words, selectively select a line to replace, or even negate it. Either way, this is the easiest part about SED.
Happy coding!
The above is the detailed content of Stream Editor (SED): Basics. For more information, please follow other related articles on the PHP Chinese website!