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‘ myfile
i
The 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