sed介绍
流编辑器,过滤和替换文本。
工作原理:sed 命令将当前处理的行读入模式空间进行处理,处理完把结果输出,并清空模式空间。然后再将下一行读入模式空间进行处理输出,以此类推,直到最后一行。这个流程理解起来非常困难,我认为这是三剑客最难理解的。
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
sed [选项] '地址 命令' file
选项 | 描述 |
---|---|
-n | 不打印模式空间 |
-e | 执行脚本、表达式来处理 |
-f | 脚本文件的内容添加到命令被执行 |
-i | 修改原文件 |
-r | 使用扩展正则表达式 |
命令 | 描述 |
---|---|
s/regexp/replacement/ | 替换字符串 |
p | 打印当前模式空间 |
P | 打印模式空间的第一行 |
d | 删除模式空间,开始下一个循环 |
D | 删除模式空间的第一行,开始下一个循环 |
= | 打印当前行号 |
a \text | 当前行追加文本 |
i \text | 当前行上面插入文本 |
c \text | 所选行替换新文本 |
q | 立即退出 sed 脚本 |
r | 追加文本来自文件 |
: label | label 为 b 和 t 命令 |
b label | 分支到脚本中带有标签的位置,如果分支不存在则分支到脚本的末尾 |
t label | 如果 s///是一个成功的替换,才跳转到标签 |
h H | 复制/追加模式空间到保持空间 |
g G | 复制/追加保持空间到模式空间 |
x | 交换模式空间和保持空间内容 |
l | 列出当前行在 |
n N | 读取/追加下一行输入到模式空间 |
w filename | 写入当前模式空间到文件 |
& | 引用已匹配字符串 |
地址 | 描述 |
first~step | 步长,每 step 行,从第 first 开始 |
$ | 匹配最后一行 |
/regexp/ | 正则表达式匹配行 |
number | 只匹配指定行 |
addr1,addr2 | 开始匹配 addr1 行开始,直接 addr2 行结束 |
addr1,+N | 从 addr1 行开始,向后的 N 行 |
addr1,~N | 从 addr1 行开始,到 N 行结束 |
$p | 打印最后一行 |
操作文件
[root@bigdata-sg-a-07]# tail /etc/services
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
示例
匹配打印(p)
打印匹配 blp5 开头的行
根据上边那个"sed [选项] '地址 命令' file"的使用方式,-n为选项,这里没有用到地址,默认就是每行读入模式空间,然后在模式空间中执行的命令为"/^blp5/"
正则匹配模式空间中的内容时候能匹配到"^blp5"
,然后通过p打印当前模式空间
[root@bigdata-sg-a-07]# tail /etc/services |sed -n '/^blp5/p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
如果不加-n选项,就会把所有读入模式空间的内容也输出一下,可以看一下
[root@bigdata-sg-a-07]# tail /etc/services |sed '/^blp5/p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
可以看到'blp5 48129/tcp # Bloomberg locator'连续打印了两次,一次是模式空间中的,一次是通过p命令输出的。 我们可以通过sedsed来看一下这个流程
[root@bigdata-sg-a-07]# tail /etc/services |./sedsed -d -n '/^blp5/p'
PATT:nimgtw 48003/udp # Nimbus Gateway$
HOLD:$
COMM:/^blp5/ p
PATT:nimgtw 48003/udp # Nimbus Gateway$
HOLD:$
省略部分
PATT:blp5 48129/udp # Bloomberg locator$
HOLD:$
COMM:/^blp5/ p
blp5 48129/udp # Bloomberg locator
PATT:blp5 48129/udp # Bloomberg locator$
HOLD:$
省略部分
nimgtw 48003/udp # Nimbus Gateway$
模式空间在读入执行"/^blp5/ p"匹配并输出并没有什么结果 而blp5 48129/udp # Bloomberg locator$
模式空间在读入执行"/^blp5/ p"匹配并输出后输出了blp5 48129/udp # Bloomberg locator
sedsed是用于sed命令debug的好工具,通过python编写,主页在http://sedsed.sourceforge.net 我们这边只说明-d参数的debug模式,还有indent、htmlize、tokenize等。
PATT:代表 读入新的一行时模式空间中的内容 HOLD:代表 读入新的一行时保持空间中的内容 COMM: 表示将要执行的命令 执行结果,这一行在执行完成后如果有就显示,如果没有就直接显示PATT PATT:代表 执行完命令后模式空间中的内容 HOLD:代表 执行完命令后保持空间中的内容
后边设计新的选项,地址和命令会继续通过这种方式来分析。
打印第一行
1为number,指匹配第一行
[root@bigdata-sg-a-07]# tail /etc/services |sed -n '1p'
nimgtw 48003/udp # Nimbus Gateway
打印第一行至第三行
1,3为addr1,addr2,代表第一行到第三行
[root@bigdata-sg-a-07]# tail /etc/services |sed -n '1,3p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
打印奇数行
1~2为first~step,从第一行开始,步长为2
[root@bigdata-sg-a-07 ~]# seq 10 |sed -n '1~2p'
1
3
5
7
9
打印匹配行及后一行
[root@bigdata-sg-a-07 ~]# tail /etc/services |sed -n '/nimgtw/,+1p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
打印最后一行
[root@bigdata-sg-a-07 ~]# tail /etc/services |sed -n '$p'
iqobject 48619/udp # iqobject
不打印最后一行
感叹号也就是对后面的命令取反
[root@bigdata-sg-a-07 ~]# tail /etc/services |sed -n '$!p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
匹配范围,以逗号分开两个样式选择某个范围
[root@bigdata-sg-a-07 ~]# tail /etc/services |sed -n '/^blp5/,/^com/p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp