Shell 通過sed替換檔案字串

2021-09-12 22:57:28 字數 603 閱讀 4206

sed "s/原字串包含'/替換字串包含'/"
sed -i ""  "s#http:/ /www.baidu.com/scm#ssh:" podconfig.rb

可以在末尾加g替換每乙個匹配的關鍵字,否則只替換每行的第乙個
」^」表示行首

」$」符號如果在引號中表示行尾,但是在引號外卻表示末行(最後一行)

# 注意這裡的 " & " 符號,如果沒有 「&」,就會直接將匹配到的字串替換掉

sed 's/^/新增的頭部&/g'      #在所有行首新增

sed 's/$/&新增的尾部/g'      #在所有行末新增

sed '2s/原字串/替換字串/g'  #替換第2行

sed '$s/原字串/替換字串/g'   #替換最後一行

sed '2,5s/原字串/替換字串/g' #替換2到5行

sed '2,$s/原字串/替換字串/g' #替換2到最後一行

sed 's/原字串/替換字串/2g'   #替換2到最後一行

sed -i 

sed 's/^/新增字串&/g;s/$/&新增的尾部/g'

sed替換文字

root localhost.localdomain home cat test 2 1492785988 int meta 0 nullable 0 is null 0 3 3598278656 longint meta 0 nullable 0 is null 0 要將test檔案中 2 149...

shell替換檔案內容

由於工作需要,需將之前文字中所記錄的 全部替換成新的 操作如下 1.找到專案,檢視有哪些檔案有該字串 grep r 52776822 2.找到這些檔案所屬的頂級目錄,然後進行替換 find name htm print0 xargs 0 sed i s 52776822 77777777 g 3.檢...

sed直接替換檔案內容

sed i s zhangsan lisi g grep zhangsan rl modules 解釋一下 i 表示inplace edit,就地修改檔案 r 表示搜尋子目錄 l 表示輸出匹配的檔名 這個命令組合很強大,要注意備份檔案。1 sed y 1234567890 abcdefghij te...