sed使用

sed命令

1、替換標記

s/pattern/replacement/flags
編輯器

默認狀況下只會替換每行的首次出現的內容,若是要替換其餘位置須要使用flagsthis

一、不使用flag

[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.

二、使用flags/pattern/replacement/flags

4種可用的替換標記:spa

  • 數字,代表新文本將替換第幾處模式匹配的地方;
  • g,代表新文本將會替換全部匹配的文本;
  • p,代表原先行的內容要打印出來;
  • w file,將替換的結果寫到文件中。 

1)數字型,好比數字2,替換第2次出現的內容,不會修改原文件

[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

2)g,替換全部的行,不會修改原文件

[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

3)w file,將替換的結果寫到新文件中,不會替換原文件

[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

4)pp替換標記會打印與替換命令中指定的模式匹配的行

[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

2、使用地址 

默認狀況下,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.

相關文章
相關標籤/搜索