由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

sed 匹配编辑

Sed substitute

  1. ## substitute the first 't' to 'T'
  2. sed 's/t/T/' file
  3. ## substitute all 't' to 'T'
  4. sed 's/t/T/g' file
  5. ## substitute 'all little letters' to 'T'
  6. sed 's/[a-z]/T/g' file
  7. ## similarly, you can substitute 'all numbers' to 'T'
  8. sed 's/[0-9]/T/g' File

Deleted matched line

  1. sed '/pattern/d' filename
  2. sed '1d' filename # deleted the first line only
  3. sed '2d' filename # deleted the second line only

Adding a line

## adding a line before the matched line
sed -i '/allow chengyongxu.com/i\allow chengyongxu.cn' file
## adding a line after the matched line
sed -i '/allow chengyongxu.com/a\allow chengyongxu.cn' file

Match pattern

Pattern Meanings Example Result
^ The begin of each line sed '/^A/d' file Delete all lines which begin with ‘A’
$ The end of each line sed 's/$/+/' file Add a ‘+’ at the end of all lines
[ ] match all single elements in it sed 's/[abcd]/T/g' file Replace all ‘a’, ‘b’, ‘c’, and ‘d’ to ‘T’

Sed

sed 's/^t/000/g' filename #the t on beginning of the lines replaced by ***
sed 's/t$/000/g' filename #000 as a substitute for the t on the end of the lines

sed 's/[0-9]/*/' filename # * as a subustitute for every number with *

sed 's/[a-z][A-Z]/--/' filename # looking for a pattern that a lower capital followed a capital
## TaT -->  T--

sed 's/[a-zA-Z]/--/g' filename # -- as a substitute for all letters on the filename
or
sed 's/[a-Z]/--/g' filename

sed 's/[0-z]/--/g' filename

sed 's/[0-9]/-&-/g' filename # exp: 833 new -- > -8--3--3- new
sed 's/[0-9][0-9]/-&-/g' filename # exp: 833 new -- > -833- new

sed 's/\w/_/g' filename # or letters except symples.(including chinese)

sed 's/\b/==/g' filename # add a "==" on the begin and the end of all words
## ==4811==  ==that==  [==ðæt==; ==ðət==]

sed 's/[^0-9]/*/g' filename # ^ means negated match

sed 's/A/B/g;s/C/D/f' FILENAME

#############################
### deleted the match line ##
#############################

sed '/\bnew\b/d' filename #boundary (new),( new),(new ),( new ),( new, )

sed '/^$/d' # remove the line which beginning with end (blank line)


sed '5,$ d' filename
equo
sed '4 q' filename
equo head -n 4 filename

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗