Common usage of sed command


sed string replacement
sed "s/original string contains'/replacement string contains'/" //characters to be processed contain single quotes

The three slash separators in the command can be replaced with other symbols, which is more convenient when there are more slashes in the content to be replaced, just follow the definition of s, for example, replace it with a question mark "?":

sed 's? Original string? Replacement string?' //Custom delimiter is question mark

You can add g at the end to replace each matching keyword, otherwise only replace the first one of each line, for example:

sed 's/original string/replacement string/' //replace all matching keywords

1. The basic syntax of sed substitution is:

sed 's/Original string/Replacement string/'
Inside single quotes, s means replacement, and the middle of three slashes is the style of replacement. Special characters need to be escaped with backslash "\".

2. There is no way to escape the single quotation marks "' '" with the backslash "\". At this time, just change the single quotation marks in the command to double quotation marks. The format is as follows:

# The characters to be processed contain single quotes
sed "s/original string contains'/replacement string contains'/" 

3. The three slash separators in the command can be replaced with other symbols. Sometimes there are more slashes when replacing directory strings. At this time, it is more convenient to replace them with other separators. Just follow the definition of s Can.

# Replace the delimiter with a question mark "?":
sed 's?Original string?Replacement string?'

4. You can add g at the end to replace each matching keyword, otherwise only replace the first one of each line, for example:

# Replace all matching keywords
sed 's/original string/replacement string/g'

5. Use of some special characters

  "^" indicates the beginning of the line.
  If the "$" symbol indicates the end of the line in quotation marks, but outside the quotation marks, it indicates the last line (the last line)
  
# Pay attention to the "&" symbol here. If there is no "&", it will directly match The string to replace sed 's/^/ added head &/g' #add sed 's/$/& added tail/g'
at the beginning of all lines #add sed '2s/original characters at the end of all lines String/replacement string/g' #replace the second line sed '$s/original string/replacement string/g' #replace the last line sed '2,5s/original string/replacement string/g' #replace 2 to 5 lines sed '2,$s/original string/replacement string/g' #replace 2 to the last line





6. Batch replace string
 

sed -i "s/find field/replace field/g" `grep find field -rl path`
sed -i "s/oldstring/newstring/g" `grep oldstring -rl yourdir

7. The output processed by sed is directly output to the screen, and the parameter "i" is used to replace it directly in the file.

# Replace all matches in the file
sed -i 's/original string/replacement string/g' filename

8. Multiple substitutions can be executed in the same command, separated by a semicolon ";", the format is:

# Execute two replacement rules at the same time
sed 's/^/Added head &/g; s/$/&Added tail/g' #Modify 
and print the modified line  
sed -i 's?root: \+\ "/bob/ssid\"?root: \"/data/ssid/model\"?g' application.yml #Modify all
 
 sed learning network
 http://c.biancheng.net/view/4028.html

Guess you like

Origin blog.csdn.net/qq_30273575/article/details/131856942