sed(Stream EDiter)作为linux文本处理三剑客之一,是一个面向行的文本处理工具(行编辑器)。
sed从一个输入流或者文件中逐行读取文本到一个名为模式空间(pattern space)的内部缓冲区中。在模式空间中,sed可以通过一个sed脚本对文本进行一个或者多个特定的操作。对于每个输入行,默认情况下,sed会先复制该行内容到模式空间,在运行完sed脚本之后,sed会输出模式空间的内容,然后再读取下一行。所以,sed默认是不会修改原文件的。如果需要直接修改原文件,需要加特定选项(-i)。
sed脚本可以在命令中被指定(使用-e选项),也可以从一个独立文件中读取(使用-f选项)。除了模式空间,sed还有另一个独立的缓冲区,名为保持空间(hold space),用来保持和累计文本。不过大多数情况下,我们所要完成的操作都在模式空间中完成。
上面提到的sed脚本不同于我们常见的shell脚本,它并不是常规意义上的一个文件,而是一个模式-动作对(也可以理解为“地址定界+编辑命令”),脚本指示出了要匹配的模式和要执行的操作:例如"2d",代表删除第二行。
OPTION | 长格式OPTION | 说明 |
---|---|---|
-n | - -quiet, - -silent | 仅显示script处理后的结果,即不再默认打印模式空间中的内容。 |
-e | script, - -expression=script | 以指定sed脚本进行文本处理,可实现多点编辑 |
-f script-file | - -file=script-file | 从文件读取sed脚本,每行是一个独立的sed命令 |
-r | - -regexp-extended | 支持使用扩展正则表达式; |
-i[SUFFIX] | , - -in-place[=SUFFIX] | 直接编辑原文件 ; |
- -help | 显示帮助 | |
- -version | 显示sed版本信息 |
定界方式 | 说明 |
---|---|
默认,空地址 | 对全文进行 |
m | 第m行 |
m,n | 从m到n的连续行 |
m,+n | 从m行到m+n行(共n+1行) |
/pattern/ | 被此模式匹配到的每一行 |
m,/pattern/ | 第m行开始到第一次被pattern匹配到的行结束 |
/pattern1/,/pattern2/ | 从第一次被pattern1匹配到的行开始,到第一次被pattern2匹配到的行结束 |
~ | 步进,例如1~2,代表所有奇数行 |
$ | 最后一行 |
编辑命令 | 说明 |
---|---|
p | 显示模式空间中的内容 |
d | 删除模式空间中的行 |
a \text | 在行后面追加文本“text”,支持使用\n实现多行追加 |
c \text | 把匹配到的行替换为此处指定的文本“text” |
i \text | 在行前面插入文本“text”,支持使用\n实现多行插入 |
s | 查找替换,其分隔符可自行指定,常用的有s///,s@@@, s###等 |
r file | 读取指定文件的内容至当前文件被模式匹配到的行后面;可实现文件合并 |
w file | 保存模式空间匹配到的行至指定的文件中(会覆盖指定文件的内容) |
! | 条件取反,格式为:地址定界!编辑命令,表示编辑命令对没有被地址定界匹配到的行发生作用,对被匹配到的行反而不起作用 |
= | 为模式匹配到的行打印行号,注意是在匹配到的行的前一行,单起一行打印行号 |
h | 把模式空间中的内容覆盖至保持空间中 |
H | 把模式空间中的内容追加至保持空间中 |
g | 把保持空间中的内容覆盖至模式空间中 |
G | 把保持空间中的内容追加至模式空间中 |
x | 把模式空间中的内容与保持空间中的内容互换 |
l | 完整显示行内容(即行尾添加行结束符) |
n | 覆盖读取匹配到的行的下一行至模式空间中 |
N | 追加读取匹配到的行的下一行至模式空间中 |
q | 退出sed |
上面提到的编辑命令中的查找替换命令(s),还有一些标记可选。
替换标记 | 说明 |
---|---|
g | 行内全局替换 |
p | 显示替换成功的行 |
w | 将替换成功的行保存至指定文件中(会覆盖指定文件中的内容) |
x | 表示互换模式空间和保持空间中的内容 |
[root@centos7u6 ~]# cat sante_fe.txt
They say that no man is an island
And good things come to those who wait
But the things I hear are there
Just to remind me
Every dog will have his day
#
And I blame this world for making a good man evil
It's this world that can drive a good man mad
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]# cat -n -E sante_fe.txt
1 They say that no man is an island$
2 And good things come to those who wait$
3 But the things I hear are there$
4 Just to remind me$
5 Every dog will have his day$
6 #$
7 $
8 And I blame this world for making a good man evil$
9 It's this world that can drive a good man mad$
10 And it's this world that turns a killer into a hero$
11 Well I blame this world for making a good man bad$
12 $
[root@centos7u6 ~]#
sed的工作模式大致是这样的:一段文本流的一行被读入到模式空间中后,会被命令所给的“模式-行为对”进行匹配:
[root@centos7u6 ~]# sed '2p' sante_fe.txt
They say that no man is an island
And good things come to those who wait
And good things come to those who wait
But the things I hear are there
Just to remind me
Every dog will have his day
#
And I blame this world for making a good man evil
It's this world that can drive a good man mad
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]#
[root@centos7u6 ~]# sed -n '2p' sante_fe.txt
And good things come to those who wait
[root@centos7u6 ~]# sed '3,12d' sante_fe.txt
They say that no man is an island
And good things come to those who wait
[root@centos7u6 ~]# sed '6i \WTF\nOMFG' sante_fe.txt
They say that no man is an island
And good things come to those who wait
But the things I hear are there
Just to remind me
Every dog will have his day
WTF
OMFG
#
And I blame this world for making a good man evil
It's this world that can drive a good man mad
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]# sed '6a \WTF\n\OMFG' sante_fe.txt
They say that no man is an island
And good things come to those who wait
But the things I hear are there
Just to remind me
Every dog will have his day
#
WTF
OMFG
And I blame this world for making a good man evil
It's this world that can drive a good man mad
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]# sed '6c \WTF\n\OMFG' sante_fe.txt
They say that no man is an island
And good things come to those who wait
But the things I hear are there
Just to remind me
Every dog will have his day
WTF
OMFG
And I blame this world for making a good man evil
It's this world that can drive a good man mad
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]
[root@centos7u6 ~]# sed 's/a/A/' sante_fe.txt
They sAy that no man is an island
And good things come to those who wAit
But the things I heAr are there
Just to remind me
Every dog will hAve his day
#
And I blAme this world for making a good man evil
It's this world thAt can drive a good man mad
And it's this world thAt turns a killer into a hero
Well I blAme this world for making a good man bad
[root@centos7u6 ~]# sed 's/a/A/g' sante_fe.txt
They sAy thAt no mAn is An islAnd
And good things come to those who wAit
But the things I heAr Are there
Just to remind me
Every dog will hAve his dAy
#
And I blAme this world for mAking A good mAn evil
It's this world thAt cAn drive A good mAn mAd
And it's this world thAt turns A killer into A hero
Well I blAme this world for mAking A good mAn bAd
[root@centos7u6 ~]# sed '1,3s/a/A/g' sante_fe.txt
They sAy thAt no mAn is An islAnd
And good things come to those who wAit
But the things I heAr Are there
Just to remind me
Every dog will have his day
#
And I blame this world for making a good man evil
It's this world that can drive a good man mad
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]# sed '1,3s/a/A/w /root/123aToA.txt' sante_fe.txt
They sAy that no man is an island
And good things come to those who wAit
But the things I heAr are there
Just to remind me
Every dog will have his day
#
And I blame this world for making a good man evil
It's this world that can drive a good man mad
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]# cat 123aToA.txt
They sAy that no man is an island
And good things come to those who wAit
But the things I heAr are there
[root@centos7u6 ~]# sed '6r 123aToA.txt' sante_fe.txt
They say that no man is an island
And good things come to those who wait
But the things I hear are there
Just to remind me
Every dog will have his day
#
They sAy that no man is an island
And good things come to those who wAit
But the things I heAr are there
And I blame this world for making a good man evil
It's this world that can drive a good man mad
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]# sed '4,6s/e/E/g w 456eToE.txt' sante_fe.txt
They say that no man is an island
And good things come to those who wait
But the things I hear are there
Just to rEmind mE
EvEry dog will havE his day
#
And I blame this world for making a good man evil
It's this world that can drive a good man mad
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]# cat 456eToE.txt
Just to rEmind mE
EvEry dog will havE his day
[root@centos7u6 ~]# sed '1,3!d' sante_fe.txt
They say that no man is an island
And good things come to those who wait
But the things I hear are there
[root@centos7u6 ~]# sed '/^And/=' sante_fe.txt
They say that no man is an island
2
And good things come to those who wait
But the things I hear are there
Just to remind me
Every dog will have his day
#
8
And I blame this world for making a good man evil
It's this world that can drive a good man mad
10
And it's this world that turns a killer into a hero
Well I blame this world for making a good man bad
[root@centos7u6 ~]# sed '/^And/q' sante_fe.txt
They say that no man is an island
And good things come to those who wait
[root@centos7u6 ~]# sed -n '1l' sante_fe.txt
They say that no man is an island$
[root@centos7u6 ~]# sed -n -e 's/^[[:upper:]]*//' -e 'l' sante_fe.txt
hey say that no man is an island$
nd good things come to those who wait$
ut the things I hear are there$
ust to remind me$
very dog will have his day$
#$
$
nd I blame this world for making a good man evil$
t's this world that can drive a good man mad$
nd it's this world that turns a killer into a hero$
ell I blame this world for making a good man bad$
$
[root@centos7u6 ~]# echo ' ' >> sante_fe.txt
[root@centos7u6 ~]# sed -n -e 's/^[[:space:]]+/not blank/' -e 'l' sante_fe.txt
They say that no man is an island$
And good things come to those who wait$
But the things I hear are there$
Just to remind me$
Every dog will have his day$
#$
$
And I blame this world for making a good man evil$
It's this world that can drive a good man mad$
And it's this world that turns a killer into a hero$
Well I blame this world for making a good man bad$
$
$
[root@centos7u6 ~]# sed -n -e 's/^[[:space:]]\+/not blank/' -e 'l' sante_fe.txt
They say that no man is an island$
And good things come to those who wait$
But the things I hear are there$
Just to remind me$
Every dog will have his day$
#$
not blank$
And I blame this world for making a good man evil$
It's this world that can drive a good man mad$
And it's this world that turns a killer into a hero$
Well I blame this world for making a good man bad$
$
not blank$
[root@centos7u6 ~]#
[root@centos7u6 ~]# sed -r -n -e 's/^[[:space:]]+/not blank/' -e 'l' sante_fe.txt
They say that no man is an island$
And good things come to those who wait$
But the things I hear are there$
Just to remind me$
Every dog will have his day$
#$
not blank$
And I blame this world for making a good man evil$
It's this world that can drive a good man mad$
And it's this world that turns a killer into a hero$
Well I blame this world for making a good man bad$
$
not blank$
+加上-r选项后,sed支持扩展正则表达式,所以’+'号不再需要转义
暂时,就先写这些,后期如果总结出一些常用的sed脚本(再次注意:这里的脚本不是指文件,而是指上面那些单引号‘’括起来的地址定界+编辑命令对),会继续为大家分享出来。