s/pattern/replacement/flags
編輯器
默認狀況下只會替換每行的首次出現的內容,若是要替換其餘位置須要使用flagsthis
[root@bogon tmp]# cat data.txt
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
[root@bogon tmp]# sed 's/test/aaa/' data.txt
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
[root@bogon tmp]# cat data.txt
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
有4種可用的替換標記:spa
[root@bogon tmp]# sed 's/test/hhhh/2' data.txt
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.
this is a test of the hhhh script.code
[root@bogon tmp]# sed 's/test/aaa/g' data.txt
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.
this is a aaa of the aaa script.blog
[root@bogon tmp]# sed 's/test/aaa/w aaa.txt' data.txt
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.
this is a aaa of the test script.ip
查看當前目錄,多了文件aaa.txt
[root@bogon tmp]# ls
aaa.txt keyring-nBhNRc orbit-gdm pulse-PDnM11f40MzK VMwareDnD
data.txt keyring-o1ipCa orbit-root pulse-tvQCaCozIrxHit
[root@bogon tmp]# cat aaa.txt
this is a aaa of the test script.
this is a sss of the test script.
[root@bogon tmp]# sed 's/aaa/test/p' aaa.txt
this is a test of the test script.
this is a test of the test script.
this is a sss of the test script.class
這一般會和sed的-n選項一塊兒使用。
$ cat data5.txt
This is a test line.
This is a different line.test
$ sed -n 's/test/trial/p' data5.txt
This is a trial line.
-n選項將禁止sed編輯器輸出。但p替換標記會輸出修改過的行。將兩者配合使用的效果就是
只輸出被替換命令修改過的行。sed
默認狀況下,sed命令做用於文本數據的全部行。若是隻想將命令做用於特定行或者某些行,則須要使用行尋址。
sed編輯器的2種行尋址方式:
[address] command
address {
command1
command2
command3
}
[root@localhost tmp]# sed '2s/test/hhh/' aaa.txt
this is a aaa of the test script.
this is a sss of the hhh script.
修改2-4行每行第2次匹配上的內容
[root@localhost tmp]# sed '2,4s/test/hhh/2' data.txt
this is a test of the test script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
修改第2行至最後一行的匹配的內容
[root@localhost tmp]# sed '2,$s/test/hhh/2' data.txt
this is a test of the test script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
this is a test of the hhh script.
[root@localhost ~]# sed '2,4{ 修改2-4行的內容
> s/test/hhh/2 修改每行的第2次匹配的內容
> s/a test/1111/ 修改每行的第1次匹配的內容
> }' /tmp/data.txt
this is a test of the test script.
this is 1111 of the hhh script.
this is 1111 of the hhh script.
this is 1111 of the hhh script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
this is a test of the test script.
[root@localhost ~]#
$ sed '/number 1/d' data6.txt
This is line number 2.This is line number 3.This is line number 4.