Shell SED Syntax detailed

Source: Internet
Author: User

SED replaces a string in a file

Sed-e ' s/foo/bar/' myfile replaces the first occurrence of each line in the MyFile file foo with a string bar , and then outputs the file contents to the standard output

Sed-e ' s/foo/bar/g ' myfile g allows SED to replace all matching strings in the file

sed -i ‘s/foo/bar/g‘ myfileiThe purpose of the option is to replace it directly in the file

To prevent the catastrophic consequences of misoperation, SED can automatically back up files before replacing them, provided that a suffix name is required. Mac OSX is mandatory backup, under CentOS is optional
sed -i ‘.bak‘ ‘s/foo/bar/g‘ ./m*
If you do not need to back up the file and use an empty string to cancel the backup, Mac OSX can use the following command to complete the replacement operation:
sed -i ‘‘ ‘s/foo/bar/g‘ ./m*

Sed ' s/^/added head &/g '//Add sed at all beginning of the line ' 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 the last line sed ' 2,5s/original string/replace string/g '//replace 2 to 5 lines sed ' 2, $s/original string/replace 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:

Sed ' s/^/added header &/g;s/$/& added tail/g '//simultaneous execution of two substitution rules

When the replaced content has variables, you need to use the ""

such as: sed "s/$a/$b/" filename

Common string substitution:

1. Replace sdk.dir=h:\\sdk.text need to replace H:\\sdk.text with/USERS/JENKINS/JENKINS/TOOL/ANDROID-SDK-MACOSX

Sed-i "" [Email protected]\ (sdk.dir=\). *@\1\/users/jenkins/jenkins/tool/[email protected] ' ${workspace}${fileload}

The delimiter after---s can be/@ #等: * Represents sdk.dir= Content \1\ representative sdk.dir=

2. Replace Thinkive.keystore to/users/jenkins/documents/android_keystore/thinkive.keystore

Sed-i "' [email protected]@/users/jenkins/documents/android_keystore/[email protected] ' ${workspace}/app/ Build.gradle

Direct replacement.

3. Replacement <string>23</string> for <string>24</string>

SH "sed-i" "" [Email protected]<string>23</string>@<string>${codeversion}</string>@ "${ Workspace}${fileload}

Direct replacement.

4. Replace the string in the special string. If 1003 in replacement thinkiveQWERT1003 is 1004

Sed-i "" '/thinkiveqwert1003/s/1003/1004/' ${workspace}/app/build.gradle

The original string is written in front of S.

Shell SED Syntax detailed

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.