Replacing string summary with sed commands in Linux

Source: Internet
Author: User
This article mainly introduces the summary of replacing the string with the sed command in Linux. it is a personal summary. if you need it, refer

I recently wrote several small scripts and used the sed command. I learned it and noted it down by the way.

The basic syntax for sed replacement is:

Copy codeThe code is as follows: sed's/original string/replacement string /'
In single quotes, "s" indicates replacement, and "\" indicates Replacement. escape is required for special characters, however, there is no way to escape the single quotation mark (') with the backslash (\). In this case, you only need to change the single quotation mark in the command to double quotation marks. for example:

Copy codeThe code is as follows: sed "s/the original string contains '/the replacement string contains'/" // the character to be processed contains single quotation marks
The three slash separators in the command can be replaced with other symbols, which is more convenient in the content to be replaced. you only need to keep up with the definition of s, for example, change to question mark "?" :

Copy codeThe code is as follows: sed's? Original string? Replace string? '// The Custom separator is a question mark
You can add g at the end to replace each matching keyword. Otherwise, only the first keyword in each line is replaced. for example:

Copy codeThe code is as follows: sed's/original string/replace string/'// replace all matching keywords
The up arrow "^" indicates the beginning of the line, and the dollar "$" indicates the end of the line in quotation marks, but it indicates the end of the line (the last line) out of quotation marks. here, it is two, which of the following symbols indicates the first line after searching for half a day? the first line is the number "1. adding a string at the beginning and end of a row replaces the end and end of a row. for example:

Copy codeThe code is as follows: sed's/^/add the header &/g' // add it at the beginning of all rows
Sed's/$/& added tail/g' // add at the end of all rows
Sed '2s/original string/replacement string/g' // replace row 2nd
Sed '$ s/original string/replacement string/g' // replace the last line
Sed '2, 5S/original string/replace string/g' // replace 2 to 5 rows
Sed '2, $ s/original string/replace string/g' // replace 2 to the last line
You can run multiple replacement styles in the same command and separate them with semicolons (;). For example:

Copy codeThe code is as follows: sed's/^/added header &/g; s/$/& added tail/g' // execute two replacement rules at the same time
Sed processed output is directly output to the screen. to save the output, you can redirect the output, or use the "I" parameter to replace it directly in the file:

Copy codeThe code is as follows: sed-I's/original string/replace string/g' filename // replace all the matches in the file
I have finished writing at a.m. and I am not sleepy. if the intermediate language is incoherent, please forgive me. thank you for your attention.

Related Article

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.