设为首页 加入收藏

TOP

【linux相识相知】sed命令(三)
2017-10-13 10:36:33 】 浏览:9535
Tags:linux 相识 相知 sed 命令
ngth you will gain View Code

在例5中,每一行都被打印了2次,原因是在模式空间的时候被打印了一次,然后处理完成之后,在标准输出又被打印了一次,所以自然会显示出两次。如果使用-n,则仅仅打印的模式空间的内容。

a test:在行后面追加文本"test",可以使用\n实现多行追加

[root@localhost ~]# sed '1 a hi i am here\nhey ' poetry 
1)Never give up,  
hi i am here
hey 
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 

i test:在 行前面插入文本test,支持使用\n实现多行插入

[root@localhost ~]# sed '2i  hi i am here\nhey ' poetry 
1)Never give up,  
hi i am here
hey 
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 

c test:把匹配到的行替换为此处指定的行

[root@localhost ~]# sed '2c  hi i am here ' poetry 
1)Never give up,  
hi i am here 
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 

w /path/to/somefile:保存模式空间中匹配到的内容到指定的文件中

[root@localhost ~]# sed '/So/  w  /tmp/poetry' 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 ~]# 
[root@localhost ~]# cat /tmp/poetry 
9)So put on a smile,  

r /path/from/somefile:读取指定文件的内容至当前文件中被模式匹配的行后面

[root@localhost ~]# cat poetry2.txt 
13)yes! I can!
[root@localhost ~]# sed '12 r poetry2.txt' 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 
13)yes! I can!

=:为模式匹配到的行打印行号,打印在行的上面

[root@localhost ~]# sed '/Never/ =' poetry
1
1)Never give up,  
2
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 '/Never/ !d' poetry  #不删除被默认匹配的行
1)Never give up,  
2)Never lose hope.  

s///:查找替换,分割符可以自定义,比如使用s@@@flag或者s###flag,默认会替换被匹配的第一个位置,但是可以添加替换标记:

s///g:全局替换

[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 's/\<you\>/your/g' poetry
1)Never give up,  
2)Never lose hope.  
3)Always have faith,  
4)It allows your to cope.  
5)Trying times will pass,  
6)As they always do.  
7)Just have pat
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇CentOS 7 更改网卡名为eth0 下一篇用SecureCRT连接虚拟机中的Linux..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目