设为首页 加入收藏

TOP

【linux相识相知】sed命令(二)
2017-10-13 10:36:33 】 浏览:9533
Tags:linux 相识 相知 sed 命令
第二行的时候,会删除第二行
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 stre
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇CentOS 7 更改网卡名为eth0 下一篇用SecureCRT连接虚拟机中的Linux..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目