This article from http://maketecheasier.com/beginners-guide-to-sed-linux/2012/03/29
After grep, the next logical step isTutorialOn sed. the SED command comes from stream editor, and as its name indicates, it deals with text flux. however, if SED is one of the most powerful commands in UNIX, its manual page is also among the most enigmatic. I will try in this article to summarize the most basic usage of SED and then give you a few examples of advanced scripts.
Basics
The GeneralCommandFor SED is something like:
Sed [Option] '{Script }' [TextFile]
SED will perform the operations that you want him to do on the text file and display the result in the standard output. If you want the result inTextFile, You can either redirect it via the habitual method:
Sed [Option] '{Script }' [TextFile] > [Edited textFile]
Or use the option"-I
"That will directly edit the input file:
Sed-I[Option] '{Script }' [TextFile]
Now let's begin working on the script. The most obvious first step is the null script:
Sed ''Test.txt
Will just display the text in test.Txt.
A good usage of SED is deletion. Let's practice through examples.
Sed '2, 4 d'Test.txt
Will delete the lines 2 to 4 of test.txt.
You can guess that the syntax for the script is:
Sed '[First line to delete] [last line to delete] D'Test.txt
But the fancy part comes when you useRegularExpressions, Or RegEx, as delimiter for the deletion. For example,
Sed '/^ #/D'Test.txt
Will delete every line that begins with "#" (in other words, if you code, it will delete all your comments ).
The general syntax is
Sed '/RegEx/d'Test.txt
For deleting the line containing the RegEx.
Sed '/Regex1/,/regex2/d'Test.txt
For deleting the interval from the line containing regex1 to the line containing regex2.
The special character "^" that I used in the first example is to indicate the beginning of the line.
Then, the second basic usage that I can think of is substitution. The general syntax is:
Sed -Re 'S/regex1/regex2 /'Test.txt
It will have for effect to search in the first line for regex1, replace it with regex2, go to the next line and repeat until the end of the entry flux.
A good example is:
Sed -Re 'S/^ #*//'Test.txt
It will replace the symbol "#" at the beginning of a line, and all the blank spaces with nothing. in other terms, it uncomments the text file. the symbol "*" is a meta-character designing 0 or more blank spaces here.
Advanced
You can do some pretty fancy stuff with SED, but you will reach the limit pretty fast if you don't pay attention to its basic behavior. sed deals with flux linearly: it applies a line-by-line treatment to a text file. if you want to do more than one modification to a same line, you have to use labels and multi-line treatment. all of this can become very complex, very quickly. I will now show you a few advanced examples and explain them to you. if you want more, I am sure that you can search by yourself and use the basics I gave you.
If you want to delete the empty lines of a file, you can use the command
Sed -Re '/^ $/{N; d }'Test.txt
The meta-character "$" means the end of the line, so "^ $" designs an empty line. then, "{n; d}" is a rather complex syntax for saying delete that line.
If you want to delete every tag in a HTML file, this is the command for you:
Sed -Re ': Start S/<[^>] *> // G;/</{n; B Start }'Test.txt
The ": Start" is called a label. it is a bit like a tag within the script that we want to go back to later in order to apply multiple changes to a same line. sed searches for anything of the form "<XXX>" (The RegEx <[^>] *>) and replaces it with nothing, so the first HTML tag of the line is deleted. but then, before going to the next line, it checks if there is something else beginning with "<", and if there is, it goes back to the label ": start "and re-applies the treatment.
Conclusion
You are now ready to study more deeply SED, or just use it for simple modifications. it is a command that I find maid in scripts in general, but it took me some time to understand its syntax. I hope it will be much faster for you.
Do you know another basic command for sed? Or do you use another advanced script involving sed that you want to share? Please let us know in the comments.