sed操作实例(一)中列举了替换,删除,插入,追加,更改的sed基本操作实例,现在继续通过实例来学习应用sed, 列表命令l,置换命令y,打印行号变量。
- 1:2:3:a:b:c:A:B:C:1:2:3
- 1:2:3:4:a:b:c:d:A:B:C:D:1:2:3:4
- 1:2:3:4:5:a:b:c:d:e:A:B:C:D:E:1:2:3:4:5
- 1:2:3:4:5:6:a:b:c:d:e:f:A:B:C:D:E:F:1:2:3:4:5:6
- 1:2:3:4:5:6:7:a:b:c:d:e:f:g:A:B:C:D:E:F:G:1:2:3:4:5:6:7
- 1:2:3:4:5:6:7:8:a:b:c:d:e:f:g:h:A:B:C:D:E:F:G:H:1:2:3:4:5:6:7:8
- ^M
- ^]
·去除非打印字符,列表命令'l'显示模式空间的内容,并将非打印字符显示为2个数字的ASCll字符
- [root@testserver ~]# sed -n 'l' test.txt
- 1:2:3:a:b:c:A:B:C:1:2:3$
- 1:2:3:4:a:b:c:d:A:B:C:D:1:2:3:4$
- 1:2:3:4:5:a:b:c:d:e:A:B:C:D:E:1:2:3:4:5$
- 1:2:3:4:5:6:a:b:c:d:e:f:A:B:C:D:E:F:1:2:3:4:5:6$
- 1:2:3:4:5:6:7:a:b:c:d:e:f:g:A:B:C:D:E:F:G:1:2:3:4:5:6:7$
- 1:2:3:4:5:6:7:8:a:b:c:d:e:f:g:h:A:B:C:D:E:F:G:H:1:2:3:4:5:6:7:8$
- \r$
- \035$
- [root@testserver ~]#
- [root@testserver ~]#
- [root@testserver ~]# sed 's/^M/999/' test.txt
- 1:2:3:a:b:c:A:B:C:1:2:3
- 1:2:3:4:a:b:c:d:A:B:C:D:1:2:3:4
- 1:2:3:4:5:a:b:c:d:e:A:B:C:D:E:1:2:3:4:5
- 1:2:3:4:5:6:a:b:c:d:e:f:A:B:C:D:E:F:1:2:3:4:5:6
- 1:2:3:4:5:6:7:a:b:c:d:e:f:g:A:B:C:D:E:F:G:1:2:3:4:5:6:7
- 1:2:3:4:5:6:7:8:a:b:c:d:e:f:g:h:A:B:C:D:E:F:G:H:1:2:3:4:5:6:7:8
- 999
- [root@testserver ~]
·置换命令y和替换命令有所不同,y是将符合的所以字符都替换,s是只针对特指的字符串完全匹配后替换
- [root@testserver ~]# sed 'y/1:/1@/' test.txt #把:都转换为@,y/左/右/,左右字符要等长
- 1@2@3@a@b@c@A@B@C@1@2@3
- 1@2@3@4@a@b@c@d@A@B@C@D@1@2@3@4
- 1@2@3@4@5@a@b@c@d@e@A@B@C@D@E@1@2@3@4@5
- 1@2@3@4@5@6@a@b@c@d@e@f@A@B@C@D@E@F@1@2@3@4@5@6
- 1@2@3@4@5@6@7@a@b@c@d@e@f@g@A@B@C@D@E@F@G@1@2@3@4@5@6@7
- 1@2@3@4@5@6@7@8@a@b@c@d@e@f@g@h@A@B@C@D@E@F@G@H@1@2@3@4@5@6@7@8
- [root@testserver ~]# sed 's/1:/1@/' test.txt #匹配到1:后才进行替换
- 1@2:3:a:b:c:A:B:C:1:2:3
- 1@2:3:4:a:b:c:d:A:B:C:D:1:2:3:4
- 1@2:3:4:5:a:b:c:d:e:A:B:C:D:E:1:2:3:4:5
- 1@2:3:4:5:6:a:b:c:d:e:f:A:B:C:D:E:F:1:2:3:4:5:6
- 1@2:3:4:5:6:7:a:b:c:d:e:f:g:A:B:C:D:E:F:G:1:2:3:4:5:6:7
- 1@2:3:4:5:6:7:8:a:b:c:d:e:f:g:h:A:B:C:D:E:F:G:H:1:2:3:4:5:6:7:8
- [root@testserver ~]# sed 's/abc/ABC/' test.txt #匹配不到abc字符串,没有替换发生
- 1:2:3:a:b:c:A:B:C:1:2:3
- 1:2:3:4:a:b:c:d:A:B:C:D:1:2:3:4
- 1:2:3:4:5:a:b:c:d:e:A:B:C:D:E:1:2:3:4:5
- 1:2:3:4:5:6:a:b:c:d:e:f:A:B:C:D:E:F:1:2:3:4:5:6
- 1:2:3:4:5:6:7:a:b:c:d:e:f:g:A:B:C:D:E:F:G:1:2:3:4:5:6:7
- 1:2:3:4:5:6:7:8:a:b:c:d:e:f:g:h:A:B:C:D:E:F:G:H:1:2:3:4:5:6:7:8
- [root@testserver ~]# sed 'y/abc/ABC/' test.txt #凡是有a就替换A,有c就替换B,有c就替换C
- 1:2:3:A:B:C:A:B:C:1:2:3
- 1:2:3:4:A:B:C:d:A:B:C:D:1:2:3:4
- 1:2:3:4:5:A:B:C:d:e:A:B:C:D:E:1:2:3:4:5
- 1:2:3:4:5:6:A:B:C:d:e:f:A:B:C:D:E:F:1:2:3:4:5:6
- 1:2:3:4:5:6:7:A:B:C:d:e:f:g:A:B:C:D:E:F:G:1:2:3:4:5:6:7
- 1:2:3:4:5:6:7:8:A:B:C:d:e:f:g:h:A:B:C:D:E:F:G:H:1:2:3:4:5:6:7:8
·打印命令p和其行号变量=
- [root@testserver ~]# sed -n '1,${=;p}' test.txt
- 1
- 1:2:3:a:b:c:A:B:C:1:2:3
- 2
- 1:2:3:4:a:b:c:d:A:B:C:D:1:2:3:4
- 3
- 1:2:3:4:5:a:b:c:d:e:A:B:C:D:E:1:2:3:4:5
- 4
- 1:2:3:4:5:6:a:b:c:d:e:f:A:B:C:D:E:F:1:2:3:4:5:6
- 5
- 1:2:3:4:5:6:7:a:b:c:d:e:f:g:A:B:C:D:E:F:G:1:2:3:4:5:6:7
- 6
- 1:2:3:4:5:6:7:8:a:b:c:d:e:f:g:h:A:B:C:D:E:F:G:H:1:2:3:4:5:6:7:8
- 7
- 8
- [root@testserver ~]
·退出命令q,匹配后终止继续往下匹配,不能和-i进行操作,防止对q之后的原文件进行损坏。
- [root@testserver ~]# sed '3q' test.txt
- 1:2:3:a:b:c:A:B:C:1:2:3
- 1:2:3:4:a:b:c:d:A:B:C:D:1:2:3:4
- 1:2:3:4:5:a:b:c:d:e:A:B:C:D:E:1:2:3:4:5
阅读(1201) | 评论(0) | 转发(0) |