每天一个Linux命令之三剑客sed
sed:流编辑工具,用来过滤和替换文本。
描述: sed是通过每次读取一行内容进行处理之后输出。首先sed是通过文件或者管道来读取文件内容的,但是sed默认不修改源文件,而是将文件内容复制到缓冲区(模式空间pattrtn space),所有的操作都是在模式空间中进行的,然后sed会根据相应的指令对模式空间的内容处理并输出。
语法: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
选项:
-f 从文件中读取脚本指令。
-n 静默输出,屏蔽自动打印模式空间的内容(默认sed在执行完后将自动打印模式空间的内容 )。
-e 可以扩展执行多个脚本
-i 修改源文件
-r 使用扩展的正则表达式
命令选项:
a \text 追加text文本
i \text 插入text文本
c \texy 用text替换所选的行
p 打印当前模式空间
d 删除模式空间
= 打印当前行编号
q 退出sed命令
r filename 追加来自文件的文本
n 读取/追加下一行到模式空间
s/regexp/replacement/ 字符串替换
w filename 把当前模式空间的内容写到文件
地址选项:
first~step 步长,从first开始每次为step步
$ 匹配最后一行
/regexp/ 正则表达式匹配行
addr1,addr2 开始匹配addr1行开始,直到addr2 行结束
addr1,+N 匹配从addr1开始到后面的N行
addr1,~N 匹配从addr1行开始到第N行结束
例子:
本例程用到的文本:
[root@python ~]# cat test.txt 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocolisnetserv 48128/udp # Image Systems Network Servicesblp5 48129/tcp # Bloomberg locatorblp5 48129/udp # Bloomberg locatorcom-bardac-dw 48556/udp # com-bardac-dwiqobject 48619/tcp # iqobject
匹配打印相应的行
#打印第一行 [root@python ~]# nl test.txt|sed -n '1p'1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol#打印奇数行[root@python ~]# nl test.txt|sed -n '1~2p'1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol3 blp5 48129/tcp # Bloomberg locator5 com-bardac-dw 48556/udp # com-bardac-dw#打印1和后面的两行[root@python ~]# nl test.txt|sed -n '1,+2p'1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol2 isnetserv 48128/udp # Image Systems Network Services3 blp5 48129/tcp # Bloomberg locator#从第一行开始打印到第二行结束(注意区别上一个例子)[root@python ~]# nl test.txt|sed -n '1,~2p'1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol2 isnetserv 48128/udp # Image Systems Network Services#打印最后一行[root@python ~]# nl test.txt|sed -n '$p' 6 iqobject 48619/tcp # iqobject#打印出有blp5的行[root@python ~]# nl test.txt|sed -n '/blp5/p'3 blp5 48129/tcp # Bloomberg locator4 blp5 48129/udp # Bloomberg locator
a: 追加 i: 插入 c: 替换 d: 删除操作
#在第二行后面追加hello,world[root@python ~]# nl test.txt|sed '2a \hello,world' 1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol2 isnetserv 48128/udp # Image Systems Network Serviceshello,world3 blp5 48129/tcp # Bloomberg locator4 blp5 48129/udp # Bloomberg locator5 com-bardac-dw 48556/udp # com-bardac-dw6 iqobject 48619/tcp # iqobject#在第二行前面插入hello,world(注意和上一个例子的区别)[root@python ~]# nl test.txt|sed '2i \hello,world' 1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocolhello,world2 isnetserv 48128/udp # Image Systems Network Services3 blp5 48129/tcp # Bloomberg locator4 blp5 48129/udp # Bloomberg locator5 com-bardac-dw 48556/udp # com-bardac-dw6 iqobject 48619/tcp # iqobject#将第二行替换成hello,world[root@python ~]# nl test.txt|sed '2c \hello,world' 1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocolhello,world3 blp5 48129/tcp # Bloomberg locator4 blp5 48129/udp # Bloomberg locator5 com-bardac-dw 48556/udp # com-bardac-dw6 iqobject 48619/tcp # iqobject#将二到六行全部替换成hello,world[root@python ~]# nl test.txt|sed '2,6c \hello,world' 1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocolhello,world#删除第二行[root@python ~]# nl test.txt|sed '2d'1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol3 blp5 48129/tcp # Bloomberg locator4 blp5 48129/udp # Bloomberg locator5 com-bardac-dw 48556/udp # com-bardac-dw6 iqobject 48619/tcp # iqobject#删除偶数行[root@python ~]# nl test.txt|sed '2~2d'1 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol3 blp5 48129/tcp # Bloomberg locator5 com-bardac-dw 48556/udp # com-bardac-dw#删除blp5和3gpp的行[root@python ~]# nl test.txt|sed '/blp5/d;/3gpp/d'2 isnetserv 48128/udp # Image Systems Network Services5 com-bardac-dw 48556/udp # com-bardac-dw6 iqobject 48619/tcp # iqobject
n :读取/追加下一行到模式空间
#打印奇数行[root@python ~]# seq 6|sed -n 'p;n'135#打印偶数行[root@python ~]# seq 6|sed -n 'n;p'246#打印出匹配的后一行[root@python ~]# nl test.txt|sed -n '/3gpp/{n;p}' 2 isnetserv 48128/udp # Image Systems Network Services
s/text1/text2/ 文本替换
#将blp5替换成http[root@python ~]# sed 's/blp5/http/' test.txt 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol isnetserv 48128/udp # Image Systems Network Serviceshttp 48129/tcp # Bloomberg locatorhttp 48129/udp # Bloomberg locatorcom-bardac-dw 48556/udp # com-bardac-dwiqobject 48619/tcp # iqobject#只打印出替换的内容[root@python ~]# sed -n 's/blp5/http/p' test.txt http 48129/tcp # Bloomberg locatorhttp 48129/udp # Bloomberg locator#使用&来代替前面要替换的内容 [root@python ~]# sed 's/blp5/&###/' test.txt 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocolisnetserv 48128/udp # Image Systems Network Servicesblp5### 48129/tcp # Bloomberg locatorblp5### 48129/udp # Bloomberg locatorcom-bardac-dw 48556/udp # com-bardac-dwiqobject 48619/tcp # iqobject#多次替换 [root@python ~]# sed 's/blp5/http/;s/iqobject/www/' test.txt 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocolisnetserv 48128/udp # Image Systems Network Serviceshttp 48129/tcp # Bloomberg locatorhttp 48129/udp # Bloomberg locatorcom-bardac-dw 48556/udp # com-bardac-dwwww 48619/tcp # iqobject#取IP地址[root@python ~]# ifconfig eth0|sed -n '2p'|sed 's/.*addr://;s/ Bcast.*//'192.168.1.13
/( )/\n/格式其中\n就是代表引用前面括号的内容。\1就是第一个括号的内容,依次类推。
#取IP地址[root@python ~]# ifconfig eth0|sed -n '/Bcast/p'|sed "s/.*addr:\([0-9.]\+\).*/\1/"192.168.1.13#取出IP地址,广播地址,和子网掩码[root@python ~]# ifconfig eth0|sed -n '/Bcast/p'|sed "s/.*addr:\([0-9.]\+\) Bcast:\([1-9.]\+\) Mask:\([0-9.]\)/IP: \1\nBCAST:\2\nMASK: \3/"IP: 192.168.1.13BCAST:192.168.1.255MASK: 255.255.255.0
= 打印行编号
[root@python ~]# sed '=' test.txt 13gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol2isnetserv 48128/udp # Image Systems Network Services3blp5 48129/tcp # Bloomberg locator4blp5 48129/udp # Bloomberg locator5com-bardac-dw 48556/udp # com-bardac-dw6iqobject 48619/tcp # iqobject#打印文件的总行数[root@python ~]# sed -n '$=' test.txt 6
-e 扩展执行多个脚本。可用;来代替
[root@python ~]# ifconfig eth0|sed -n '2p'|sed -e 's/.*addr://' -e 's/ Bcast.*//'192.168.1.13用;代替-e [root@python ~]# ifconfig eth0|sed -n '2p'|sed 's/.*addr://;s/ Bcast.*//'192.168.1.13
w 将匹配行写入到文件中
[root@python ~]# tail test.txt |sed '/blp5/w test2.txt' 3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocolisnetserv 48128/udp # Image Systems Network Servicesblp5 48129/tcp # Bloomberg locatorblp5 48129/udp # Bloomberg locatorcom-bardac-dw 48556/udp # com-bardac-dwiqobject 48619/tcp # iqobject[root@python ~]# cat test2.txt blp5 48129/tcp # Bloomberg locatorblp5 48129/udp # Bloomberg locator
10.实现在指定的行后面添加内容并写入到文件中。
[root@localhost shell.sh]# cat test.sh [fruit] apple [root@localhost shell.sh]# sed -i "/apple/a\watermelon" test.sh [root@localhost shell.sh]# cat test.sh [fruit] apple watermelon [root@localhost shell.sh]# sed -i "/apple/a\orange\nstrawberry" test.sh [root@localhost shell.sh]# cat test.sh [fruit] apple orange strawberry watermelon |
2017/4/23 15:07:48