1、本篇從刪除行開始講起shell
文本替換命令只是sed編輯器多種命令用法之一,若是咱們工做當中,須要刪除文本中特定的文本行或者字符串,那就要用到sed編輯器的刪除命令了.bash
刪除命令爲d,它將刪除全部與命令匹配的文本行或字符串。使用刪除命令要當心,由於在命令執行的過程當中包含尋址模式,若是忘記尋址模式,全部文本行都會從文本流中刪除。編輯器
- $ sed 'd' test2.txt
注意,上述命令我只是掩飾d的用法,加上-i參數,才能真正刪除整個文本的內容。切忌,刪除前,必定要記得用尋址模式,下面咱們演示利用尋址模式,刪除文本中特定的行。ide
- $ sed '3d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
發現沒,文本中共有五行,刪除了第三行,因此就變成四行了。this
再來作個刪除特定文本行範圍的實例:spa
- $ sed '2,3d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
使用特殊的文件結束符,來指定要刪除的一段文本行命令行
- $ sed '3,$d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
sed編輯器的模式匹配功能也一樣適用於刪除命令3d
- sed '/number 1/d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
這一步要注意,sed編輯器不會處理原始文件,所刪除的全部文本行僅從sed編輯器輸出中刪除,原始文件仍然包含這些「已刪除的行」。
xml
再來練習一個刪除指定行範圍的例子ip
- $ sed '/1/,/3/d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
2、插入和附加文本
與其餘編輯器同樣,sed編輯器也能夠在數據流中插入和附加文本。兩個操做之間有必定的差異,千萬不要搞混淆了哦
插入命令(i)在指定行以前添加新的一行
附加命令(a)在指定行以後添加新的一行
這兩個命令格式比較容易混淆,不能在單命令行上使用這兩個命令,必須單獨要插入和附加的行。格式以下:
- sed '[address]command\
- new line'
new line 中的文本出如今sed輸出中指定的位置,使用插入命令,文本出如今該數據流文本以前,例子以下:
- $ echo "testing"|sed 'i\
- > this is a test'
- this is a test
- testing
然而,在使用附加命令的時候,文本就出如今該數據流文本以後,示例以下:
- $ echo "testing"|sed 'a\
- > this is a test'
- testing
- this is a test
(a) ,(i)這兩個命令,也能夠用在數據流內部進行文本插入和附加處理,在特定文本行上處理:
- $ sed '3i\
- > this is a dog line test' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is a dog line test
- this is script test, we are test bash shell 3
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
在第三行,成功插入this is a dog line test
接下來,咱們在將this is a dog line test插入到第三行以後:
- $ sed '3a\
- > this is a dog line test' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is script test, we are test bash shell 3
- this is a dog line test
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
若是數據流有多行,而您又須要在此數據流末尾附加新的一行文本,那麼你可使用美圓符號($),它表示尾行,示例:
- $ sed '$a\
- > this is a dog line test' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is script test, we are test bash shell 3
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
- this is a dog line test
要插入或附加多行文本,必需要在每一行插入或附加的新文本行以前使用一個反斜槓,直到到達要插入或者附加文本行的最後一行:
- $ sed '1i\
- > this is a dog test line\
- > this is a beautiful test' test2.txt
- this is a dog test line
- this is a beautiful test
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is script test, we are test bash shell 3
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
3、更改行
更改行命令用於更改數據流中整行文本內容,工做方式與插入和附加命令相同,所以必須與其餘命令獨立執行。示例:
- $ sed '3c\
- > this is new correct line' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is new correct line
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
在本例中,sed編輯器更改了第三行文本內容,咱們也可使用文本模式進行更改:
- $ sed '/number 3/c\
- > this is new right script' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is new right script
- this is script test, we are test bash shell 5
- this is script test, we are test bash shell 5
文本模式更改命令將更改與全部文本模式匹配的文本行:
- $ sed '/number 1/c\
- > this is a beautiful test script' test2.txt
- this is a beautiful test script
- this is script test, we are test bash shell 2
- this is script test, we are test bash shell 3
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
在更改命令中,可使用地址範圍,可是結果卻不是預期要達到的結果。
- $ sed '2,3c\
- > this is a beautiful test script' test2.txt
- this is script test, we are test bash shell 1
- this is a beautiful test script
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
執行後,結果倒是把2,3兩行變成了一行更改後的文本。因此這點要注意!