MSN: Phenzer@hotmail.com 欢迎加为好友讨论测试

linux下使用最广泛的命令之一:sed(应用篇一)

上一篇 / 下一篇  2010-11-09 11:09:24 / 个人分类:Linux

如果不特别说明,sed例子中使用下述文本文本quote.txt的内容:
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

三、实例
1.使用p显示行
[root@RHEL5 shell]# sed '3p' quote.txt 
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
发现打印了所有行,只是第三行打印了2边。使用-n选项只显示匹配行,故
[root@RHEL5 shell]# sed -n '3p' quote.txt 
Too bad the disco floor fell through at 23:10.
2.打印范围
root@RHEL5 shell]# sed -n '2,4p' quote.txt 
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
3.打印模式
[root@RHEL5 shell]# sed -n '/company/p' quote.txt 
It was an evening of splendid music and company.
4.使用模式和行号
[root@RHEL5 shell]# sed -n '/The/p' quote.txt  
The honeysuckle band played all night long for only $90.
The local nurse Miss P.Neave was in attendance.
[root@RHEL5 shell]# sed -n '4,/The/p' quote.txt 
The local nurse Miss P.Neave was in attendance.
5.匹配特殊字符
[root@RHEL5 shell]# sed -n '/\$/p' quote.txt 
The honeysuckle band played all night long for only $90.
6.显示整个文件
[root@RHEL5 shell]# sed -n '1,$p' quote.txt 
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
7.任意字符
[root@RHEL5 shell]# sed -n '/.*ance/p' quote.txt   
The local nurse Miss P.Neave was in attendance.
8.显示首行
[root@RHEL5 shell]# sed -n '1p' quote.txt 
The honeysuckle band played all night long for only $90.
9.显示尾行
[root@RHEL5 shell]# sed -n '$p' quote.txt 
The local nurse Miss P.Neave was in attendance.
10.打印行和行号
[root@RHEL5 shell]# sed -n '/music/=' quote.txt 
2
只能显示行号。
[root@RHEL5 shell]# sed -e '/music/=' quote.txt  
The honeysuckle band played all night long for only $90.
2
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
显示了多余的内容。
只显示匹配的行和行号,需要使用2个sed命令。第一打印模式匹配行,第二个使用=打印行号:
[root@RHEL5 shell]# sed -n -e '/music/p' -e '/music/=' quote.txt 
It was an evening of splendid music and company.
2
11.附加文本
使用a\附加文本,可以将指定文本一行或者多行附加到指定行。如不指定文本放置的位置,sed缺省放在每一行的后面。同时附加文本不能指定范围。
[root@RHEL5 shell]# sed '/company/ a\ this line will be appended' quote.txt          
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
 this line will be appended
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
12.sed脚本文件
#!/bin/sed -f
/company/ a\
this line will be appended
-----end-----
修改权限,然后执行,功能也是实现附加文本
13.插入文本
插入和附加类似,只是在指定行前插入,也只接受一个地址,而且地址是模式或行号
格式:/pattern/ i\ 或line/Number i\ insertText
#!/bin/sed -f
/attendance/ i\
utter confusion followed、
结果:
The honeysuckle band played all night long for only $90.
It was an evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
utter confusion followed
The local nurse Miss P.Neave was in attendance.
14.修改文本
#!/bin/sed -f
/The honeysuckle/ c\
The Office Dibble band played well.
修改命令将在批评模式空间的指定行用新文本加以代替,也可以使用行号指定要修改的行。
15.删除文本
删除第一行:sed '1d' quote.txt
删除第一到第三行:sed '1,3d' quote.txt
删除最后一行:sed '$d' quote.txt
删除含'Neave'的行:sed '/Neave/' quote.txt
16.替换文本
替换night为NIGHT:
[root@RHEL5 shell]# sed -n 's/night/NIGHT/p' quote.txt 
The honeysuckle band played all NIGHT long for only $90.
删除$符合:
[root@RHEL5 shell]# sed -n 's/\$//p' quote.txt 
The honeysuckle band played all night long for only 90.
将所有The替换为Wow:
[root@RHEL5 shell]# sed -n 's/The/Wow/gp' quote.txt 
Wow honeysuckle band played all night long for only $90.
Wow local nurse Miss P.Neave was in attendance.
将替换结果写入文件:
[root@RHEL5 shell]# sed -n 's/The/Wow/gpw out.txt' quote.txt   
Wow honeysuckle band played all night long for only $90.
Wow local nurse Miss P.Neave was in attendance.  
[root@RHEL5 shell]# cat out.txt 
Wow honeysuckle band played all night long for only $90.
Wow local nurse Miss P.Neave was in attendance.

TAG: 命令 应用 sed

 

评分:0

我来说两句

Open Toolbar