sed : stream Editor
並不直接處理文件,逐行讀至模式空間,然後進行修改,默認不編輯元文本,支隊模式空間內的文本作處理
格式: sed [options] 'Address Command ' file1 file2
options :
-n : 靜默模式,不再默認顯示模式空間中的內容
-i : 直接修改源文件
-e: -e script -e script 一次指定多個腳本
-f /Path/to/script : 將指定腳本應用到某文件上
-r : 使用擴展正則
Address : 地址信息 ,指定要對那幾行進行編輯
1.startline,endline 1,100 1~100行
$ : 最後一行
$-1:倒數第二行
2./RegExp/ : 使用正則表達式
3./pattern1/,/pattern2/ 第一次被模式1匹配到的行 到 第一次被模式二匹配到的行
4,Linenumber : 精確匹配某行
5,startLine , +N : 從startLine開始後的N行( 包括當前行,一共是n+1行 )
Command :
d : delete sed'1d'
p : print 顯示符合條件的行
a /string : add string 在指定的行後面追加 string
i /string : infront string 在指定的行前面追加 string (\n可以用來換行)
r Path/file : read file 在指定行後面追加讀取某文件的內容
w Path/file : write file 將指定範圍內的內容保存到指定文件
s/pattern/string/ 修飾符 : search 查找並替換可以使用正則,默認只替換第一次被匹配到的串(可以自行指定分隔符 s#pattern#string#修飾符),支持後向引用
& : s/pattern/&string/ 將pattern匹配到的內容與string結合,再加到匹配到的行後
後向引用 : 當在string中需要使用到pattern中匹配到的部分內容時,必須用後向引用
修飾符:
g 全劇替換
i 不區分大小寫