The basic syntax for sed substitution is:
----s followed by a delimiter, the original string can be used. * This regular expression for the entire line substitution
The code is as follows:
' s/Original string/replacement string/ '
Single quotes inside, s for substitution, three slash in the middle is a replacement style, special characters need to be escaped with a backslash "\", but the single quote "'" is no way to escape with a backslash "\", this time as long as the single quotation mark in the command is changed to double quotation marks on the line, for example:
The code is as follows:
" s/The original string contains '/replace string contains '/ " //characters to be processed contain single quotation marks
The three slash delimiter in the command can be replaced by a different symbol, which is more convenient to replace with more slashes, just follow the s definition, such as the question mark "?":
The code is as follows:
' s? original string? Replace String? ' //Custom delimiter is a question mark
You can replace each matched keyword with g at the end, or replace only the first of each line, for example:
The code is as follows:
' s/Original string/replacement string/ ' //replace all matching keywords
Up ARROW "^" means the beginning of the line, the dollar "$" symbol if the end of the lines in quotation marks, but in the introduction of the last row (the final line), here committed two, searched for half a day which symbol represents the first line, half a day just remembered, the first line is the number "1″ ah." Then adding a string at :
The code is as follows:
Sed's/^/added head &/g'//add sed at all beginning's/$/& added tail/g'//add sed at the end of all lines'2s/Original string/replacement string/g'//Replace line 2nd sed'$s/original string/replacement string/g'//replace the last line of SED'2,5s/Original string/replacement string/g'//Replace 2 to 5 lines of sed'2, $s/original string/replacement string/g'Replace 2 to last line
A replacement style can be executed in more than one command, with a semicolon ";" Separation, for example:
The code is as follows:
' s/^/added header &/g;s/$/& added tail/g ' //execute two substitution rules at a time
The output processed by SED is output directly to the screen, to be saved can redirect the output, or replace it with the parameter "I" directly in the file:
The code is as follows:
' s/Original string/replacement string/g ' filename//Replace all occurrences in the file
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++
The first is the problem of using variables in sed
There are four types of programs on the Web:
1. eval sed ' s/$a/$b/' filename
2. sed "s/$a/$b/" filename
3. . Sed ' s/' $a '/' $b '/' filename
4. . sed s/$a/$b/filename
I prefer the second one, which is thatthe expression following the SED is usually enclosed in single quotation marks ('), and a double quotation mark (") is used when the variable is needed.
About the difference between single and double quotes:
Single quote:when the shell processes a command, it does nothing about the contents. That is, the content within the quotation marks is the format defined by the SED command.
Double quotation marks:when the shell processes a command, the content in it is scaled in arithmetic. If you want the shell to expand and get the format of the SED command, use the command:sed-n "/\\\\$/p" haha, the result of the extension is \\$.
So for statements like:
$Comfilename = "/home/evan/sandbox/main/"
1. Echo $Comfilename | Sed ' s#\/#\\\/#g '
2. Echo $Comfilename | Sed "s#\/#\\\/#g"
The result of the first one is:\/home\/evan\/sandbox\/main\/
And the second is:/home/evan/sandbox/main/because the double quotation marks will be "/" interpreted as "/", so the sed "s#\/#\\\/#g" was interpreted by the shell as sed s#/#\/ #g into the sed to run "/" escaped to "/", so that the equivalent of two times to explain, will not get the desired results.
This example tells me not to use double quotes when it is not necessary, otherwise you will be very depressed. Of course, the efficiency of single quotation marks is higher than double quotation marks and one of the reasons for not abusing double quotes.
How do you use the special characters that need to be escaped in the SED variables?
One way to put it online is to replace the characters in the SED that represent the substitution with the characters that are not in the other variable so that it is equivalent to the characters in the variable that are not escaped.
Such as:
Sed–i "s# $Comfilename #/root/#" filename.list
This is a good idea. But unfortunately what I need is to delete "D" instead of substituting "s". When I use the same method to delete it does not seem to play a role:sed–i "# $Comfilename #d" Filename.list
So I can only rewrite the variables by hand.
$Comfilename = "/home/evan/sandbox/main/"
Tempname= ' echo $Comfilename | Sed ' s#\/#\\\/#g ' (the result of anti-single-quote execution is given to temporary variables, and the same method can overwrite other symbols that need to be escaped.) )
Sed–i "# $ tempname #d" Filename.list
Reprint: Shell script sed uses----substitution, variable, escape character