设为首页 加入收藏

TOP

【linux相识相知】sed命令(四)
2017-10-13 10:36:33 】 浏览:9536
Tags:linux 相识 相知 sed 命令
ience,
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 your will gain View Code

sed默认支持的基本的正则表达式,使用-r, --regexp-extended选项可以支持扩展的正则表达式。

 

练习1:删除/boot/grub2/grub.cfg文件中所有以空白字符开头的行的行首的所有空白字符

[root@localhost ~]# sed -r 's@^[[:space:]]+@@g' /boot/grub2/grub.cfg
View Code

练习2:删除/etc/fstab文件中所有以#开头的行的行首的#号及#后面的所有空白字符

[root@localhost ~]# sed 's/^#[[:space:]]*//g' /etc/fstab
View Code

练习3:输出一个目录给sed,取出其目录,类似于dirname

[root@localhost ~]# echo "/var/log/messages" | sed -r 's@[^/]+/?$@@'
/var/log/
[root@localhost ~]# 
[root@localhost ~]# echo "/var/log/messages/" | sed -r 's@[^/]+/?$@@'
/var/log/
View Code

 

 高级编辑命令

 sed还有一些高级的命令,会用到之前说到的保持空间。

 

举一些例子来看一下吧!

[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 

可以自己先思考一下答案,再点开:

[root@localhost ~]# sed -n 'n;p' 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 
sed -n 'n;p' poetry
[root@localhost ~]# sed  '1!G;h;$!d'  poetry  #逆序显示
12)And strength you will gain 
11)Know it will pass,  
10)You'll live through your pain.  
9)So put on a smile,  
8)Your dreams will come true.  
7)Just have patience,  
6)As they always do.  
5)Trying times will pass,  
4)It allows you to cope.  
3)Always have faith,  
2)Never lose hope.  
1)Never give up, 
sed '1!G;h;$!d' poetry
[root@localhost ~]# sed  '$!d'  poetry  #只保留最后一行,类似tail -1
12)And strength you will gain 
sed '$!d' poetry
[root@localhost ~]# sed  '$!N;$!D' poetry  #取出最后两行
11)Know it will pass,  
12)And strength you will gain 
sed '$!N;$!D' poetry
[root@localhost ~]# sed 'G' 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 
sed 'G' poetry

这些都是一些高级的用法,在工作中用的可能比较少,因为有的结果完全可以用其他简单的命令去实现。

 

循环和分支

 sed最牛逼之处莫过于这个了,sed提供了一个循环和分支工来控制程序的执行流程。

 循环

sed循环的工作原理类似于现代编程语言中的goto语句。

定义sed的标签:

:label

跳转到标签的位置,使用b命令后面跟着标签名即可,下面我们来看一个具体的实例:

[root@localhost ~]# cat teleplay 
剧名:白夜追凶
评分:9.0
剧名:秘密深林
评分:9.3
剧名:权利的游戏第七季
评分:9.3
剧名:请回答1988
评分:9.6
[root@localhost ~]# sed -n '
h;n;H;x
s@\n@,@
/请回答/!b label
s@^@---@
:label
p' teleplay
剧名:白夜追凶,评分:9.0
剧名:秘密深林,评分:9.3
剧名:权利的游戏第七季,评分:9.3
---剧名:请回答1988,评分:9.6

分析:

由于命令很长,我们可以将命令分行来写,当然也可以写在一行里面:

1.h;n;H;x:将第一行内容读取到模式空间,执行h,将模式空间的内容覆盖至保持空间,执行n,从文件中读取第二行覆盖至模式空间,执行H,将模式空间的内容追加至保持空间,执行x,将保持空间的内容和模式空间调换;(这里得到的结果就是模式空间中存在两行);

2.s@\n@,@,将换行符替换为逗号;这样原来的两行就变成了一行;

3./请回答/!b label,判断这一行中是否有“请回答”三个字符,如果没有则直接跳到":label",然后执行打印"p",打印模式空间中的内容,如果有则执行"s@^@---@",将在行首添加字符"---"。

这样一来,只有"剧

首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇CentOS 7 更改网卡名为eth0 下一篇用SecureCRT连接虚拟机中的Linux..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目