你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux基础知识:sed命令

[日期:2017-09-16] 来源:Linux社区  作者:liubinsh [字体: ]

在之前的文章中我们介绍了文本三剑客中grep,本次博客就另外一名剑客——sed做出详细的描述,sed真的是一款强大的工具。下面让我们来一起看一下吧!

概述和工作机制

SED的英文全称为Stream EDitor,中文称流编辑器。默认情况下,它会一行一行的读取文件中的内容,在了解其工作原理之前,首先我们得先知道一下几个概念:

1.模式空间(pattern buffer):sed从文件中读取行首先会放到模式空间中进行执行和处理,定义的sed命令都是在这里执行的,默认情况下会逐行的读,逐行的处理,除非你做了行定界。

2.保持空间(hold buffer):在处理完模式空间的一些行的时候,我们有可能需要一个临时的地方去存放模式空间中的内容,这时候就可以将模式空间中的内容放到保持空间了。

初始情况下,模式空间和保持空间都是空的。

1.默认情况下,将从文件中逐行的读取内容至模式空间;

2.默认情况下,模式空间中的内容在处理完成后将会打印到标准输出;

3.sed命令在模式空间中的都是按顺序执行的,除非指定了行定界,否则将在所有的行上面执行;

4.修改后的行被送至标准输出的之后,模式空间将被清空。

基本选项

 语法格式:

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

-n:不输出模式空间中的内容至标准输出

 
#例子1
[root@localhost ~]# sed '' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Wed Sep  6 11:16:57 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=0ab24855-5180-4d3e-a61d-99ca54711c2c /boot                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0
[root@localhost ~]# 
#例子2
[root@localhost ~]# sed -n '' /etc/fstab 
[root@localhost ~]#

默认情况下,sed命令会逐行的读取文件中每一行内容至模式空间,再执行执行单引号内的命令,如例子1,单引号内没有任何内容,所以不会对行内容作任何的处理,所以sed会逐行读取文件的内容,然后逐行的显示到标准输出,每打印一行到标准输出,模式空间就会被清空一次。在例子2中,加了-n的选项,会阻止输出到标准输出,所以就不会显示任何的内容。

 -e script:指定多个命令

 
[root@localhost ~]# cat poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain
#例子3
[root@localhost ~]# sed -e '1d' -e '10d' poetry 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
11)Know it will pass,  
12)And strength you will gain 

使用-e选项,可以同时对行作出多个模式匹配,d的意思是删除。这里的例子3,文件poetry是输入,sed读取第一行的内容至模式1空间,然后判断是否是第一行,判断成功是第一行,则删除第一行(从模式空中清除,也不会打印至标准输出),读取第二行,先判断是否是第一行,不是,再判断是否是第十行,不是,然后打印至标准输出,所以在标准输出中只显示了不包含第一行和第十行的其他行的内容。注意:sed不会修改原文件的内容。

-f  /path/to/script:把sed的编辑命令放在文件中,注意每一行一个命令:

 
#例子4
[root@localhost ~]# cat sed_script.txt    #编辑命令存放的文本文件
1d
10d
[root@localhost ~]# sed -f sed_script.txt poetry 
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
11)Know it will pass,  
12)And strength you will gain 

我们可以看到例子4中sed使用-f选项指定编辑命令所在的文件,执行的是和例子3同样的操作,删除第一行和第十行,当我们得编辑命令有很多,把它放在文件也是一个不错的做法吧!

行定界

默认情况下不指定行的定界,会读取处理整个文件的每一行。如果指定了行,还是会读取每一行,但是在执行各种命令操作之后,会进行判断是不是定界的行,如果判断成功,则执行操作,如删除行,如果判断失败,则不做任何的处理然后打印至标准输出,下面来看一下几种常见的定界方法:

(1)定界为空,对全文的每一行进行处理

[root@localhost ~]# sed  '' poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
 
(2)单行指定
a.直接写单个数字,表示指定行
 
[root@localhost ~]# sed  '2d' poetry  #当判断为第二行的时候,会删除第二行
1)Never give up,  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain

b./pattern/:被模式匹配到的行,默认可以是基本的正在表达式

[root@localhost ~]# sed  '/As/ d' poetry  #删除被模式匹配的行
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

c.$最后一行

[root@localhost ~]# sed  '$ d' poetry  #删除最后一行
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,

(3)定界范围

a.x,y:x至y行

[root@localhost ~]# sed  '1,9 d' poetry  #删除了1-9行
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

b.x,+y:x行到,x行往下数y个行

[root@localhost ~]# sed  '1,+3 d' poetry #删除第一行,和第一行往下的3行,也就是一直删除到第四行
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

c.x,/pattren/:x到被模式匹配的行

[root@localhost ~]# sed  '2,/smile/ d' poetry  #删除第二行至被smile匹配的行,smile被第九行匹配
1)Never give up,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

d./pattren1/,/pattern2/:被模式1匹配的行开始到被模式2匹配的行

[root@localhost ~]# sed '/Always/,/put/ d' poetry 
1)Never give up,  
2)Never lose hope.  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 
(4)使用"~"指定步进
"~"号前面为开始行,后面为步进的长度:
 
[root@localhost ~]# sed '1~2 d' poetry  #显示偶数行
2)Never lose hope.  
4)It allows you to cope.  
6)As they always do.  
8)Your dreams will come true.  
10)You'll live through your pain.  
12)And strength you will gain 
[root@localhost ~]# sed '2~2 d' poetry   #显示奇数行
1)Never give up,  
3)Always have faith,  
5)Trying times will pass,  
7)Just have patience,  
9)So put on a smile,  
11)Know it will pass,  

编辑命令

下面我们来看一下编辑命令,如前面的d选项是删除行的意思,那么除了d选项,还有哪些呢?

p:显示模式空间的内容

#例5
[root@localhost ~]# sed   'p' poetry 
1)Never give up,  
1)Never give up,  
2)Never lose hope.  
2)Never lose hope.  
3)Always have faith,  
3)Always have faith,  
4)It allows you to cope.  
4)It allows you to cope.  
5)Trying times will pass,  
5)Trying times will pass,  
6)As they always do.  
6)As they always do.  
7)Just have patience,  
7)Just have patience,  
8)Your dreams will come true.  
8)Your dreams will come true.  
9)So put on a smile,  
9)So put on a smile,  
10)You'll live through your pain.  
10)You'll live through your pain.  
11)Know it will pass,  
11)Know it will pass,  
12)And strength you will gain 
12)And strength you will gain 
#例6
[root@localhost ~]# sed -n  'p' poetry 
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows you to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have patience,  
8)Your dreams will come true.  
9)So put on a smile,  
10)You'll live through your pain.  
11)Know it will pass,  
12)And strength you will gain 

更多详情见请继续阅读下一页的精彩内容http://www.linuxidc.com/Linux/2017-09/146906p2.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款