Replace string summary with sed command in Linux

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.