The basic syntax for sed substitution is:
sed ' s/original string/replacement string/'
Single quotation mark, s for replacement, three slash middle is a replacement style, Special characters need to be escaped with a backslash "\", but the single quote "'" is not escaped with a backslash "\", as long as the single quotation mark in the command is changed to double quotation marks, for example:
sed "s/The original string contains '/substitution string contains '/"//the character to be processed contains single quotation marks The three slash delimiter in the
command can be replaced with a different symbol, which is more convenient to replace with more slashes, just follow the s definition, such as the question mark "?":
sed ' s? The original string? '//Custom delimiter is a question mark
can replace each matching keyword at the end, or replace only the first one on each line, for example:
sed ' s/original string/replacement string/'//Replace all matching keywords
up arrow "^" For the beginning of the row, USD "$" Symbol if the end of the line 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 beginning and end of the line is replacing the end of the line with the beginning ' s/^/added head &/g '//Add
sed ' s/$/& added tail/g '//At the end of all lines add
sed ' 2s/original string/replace string/g '//replace 2nd line
sed ' $s/original string/ Replace string/g '//replace last line
Sed ' 2,5s/original string/replace string/g '//replace 2 to 5 lines
sed ' 2, $s/original string/replacement string/g '//replace 2 to last line
Replace style can be executed multiple in the same command , with a semicolon ";" Delimited, for example:
sed ' s/^/added header &/g;s/$/& added tail/g '//execute two substitution rules simultaneously
sed processed output is directly output to the screen, to save can redirect output, Alternatively, use the parameter "I" to replace the file directly:
sed-i ' s/the original string/replacement string/g ' filename//Replace all occurrences in the file
Replace string summary with sed command in Linux