sed常用命令
命令 功能
a\ 在当前行后添加一行或多行
c\ 用新文本替换当前行中的文本
i\ 在当前行前插入文本
d 删除行
g 在行内进行全局替换
p 打印行
n 读入下一行,并从下一个命令对其进行处理
r 从文件中读取输入行
! 对所选行以外的所有行应用命令
s 用一个字符串替换另一个
w 将行写入文件
y 将字符转换为另一字符
-e 允许多项编辑
-n 取消缺省的输出
-f 指定sed脚本文件
sed实例
[root@localhost script]# cat new
M.Tansley 05/99 48311 GREEN 8 40 44
J.LULU 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-2 12 30 28
Brown 08/99 5877 huangqi 45 22 24
L.Tansley 05/99 47120 Brown-2 12 30 28
1.删除第三行
[root@localhost script]# cat new|sed '3d'
M.Tansley 05/99 48311 GREEN 8 40 44
J.LULU 06/99 48317 green 9 24 26
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-2 12 30 28
Brown 08/99 5877 huangqi 45 22 24
L.Tansley 05/99 47120 Brown-2 12 30 28
2.删除第三行到最后一行
[root@localhost script]# cat new|sed '3,$d'
M.Tansley 05/99 48311 GREEN 8 40 44
J.LULU 06/99 48317 green 9 24 26
3.删除最后一行
[root@localhost script]# cat new|sed '$d'
M.Tansley 05/99 48311 GREEN 8 40 44
J.LULU 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-2 12 30 28
Brown 08/99 5877 huangqi 45 22 24
4.删除含有Brown的行
[root@localhost script]# cat new|sed '/Brown/d'
M.Tansley 05/99 48311 GREEN 8 40 44
J.LULU 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
5.将Brown全部替换为abc
[root@localhost script]# cat new|sed 's/Brown/abc/g'
M.Tansley 05/99 48311 GREEN 8 40 44
J.LULU 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 abc-3 12 26 26
L.Tansley 05/99 4712 abc-2 12 30 28
abc 08/99 5877 huangqi 45 22 24
L.Tansley 05/99 47120 abc-2 12 30 28
6.将行首为J的替换为G
[root@localhost script]# cat new|sed 's/^J/G/g'
M.Tansley 05/99 48311 GREEN 8 40 44
G.LULU 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
G.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-2 12 30 28
Brown 08/99 5877 huangqi 45 22 24
L.Tansley 05/99 47120 Brown-2 12 30 28
7.将行尾的两位数后加.5
[root@localhost script]# cat new|sed 's/[0-9][0-9]$/&.5/g'
M.Tansley 05/99 48311 GREEN 8 40 44.5
J.LULU 06/99 48317 green 9 24 26.5
P.Bunny 02/99 48 Yellow 12 35 28.5
J.Troll 07/99 4842 Brown-3 12 26 26.5
L.Tansley 05/99 4712 Brown-2 12 30 28.5
Brown 08/99 5877 huangqi 45 22 24.5
L.Tansley 05/99 47120 Brown-2 12 30 28.5
8.把Brown-2改为Brown-4
[root@localhost script]# cat new|sed 's/\(Brown-\)2/\14/g'
M.Tansley 05/99 48311 GREEN 8 40 44
J.LULU 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-4 12 30 28
Brown 08/99 5877 huangqi 45 22 24
L.Tansley 05/99 47120 Brown-4 12 30 28
9.将Brown全部替换为abc
[root@localhost script]# cat new|sed -n 's#Brown#abc#p'
J.Troll 07/99 4842 abc-3 12 26 26
L.Tansley 05/99 4712 abc-2 12 30 28
abc 08/99 5877 huangqi 45 22 24
L.Tansley 05/99 47120 abc-2 12 30 28
10.打印LULU和Brown之间的行
[root@localhost script]# cat new|sed -n '/LULU/,/Brown/p'
J.LULU 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
11.打印从第2行开始到第一次出现Brown的行
[root@localhost script]# cat new|sed -n '2,/Brown/p'
J.LULU 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
12.在Brown行尾部加上**VASA**
[root@localhost script]# cat new|sed -n '/Brown/s/$/**VASA**/p'
J.Troll 07/99 4842 Brown-3 12 26 26**VASA**
L.Tansley 05/99 4712 Brown-2 12 30 28**VASA**
Brown 08/99 5877 huangqi 45 22 24**VASA**
L.Tansley 05/99 47120 Brown-2 12 30 28**VASA**
13.将含有Brown的行写入到newfile文件中
[root@localhost script]# cat new|sed -n '/Brown/w newfile'
[root@localhost script]# cat newfile
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-2 12 30 28
Brown 08/99 5877 huangqi 45 22 24
L.Tansley 05/99 47120 Brown-2 12 30 28
14.在含有Brown行的后面加入“----------”
[root@localhost script]# cat new|sed '/Brown/a\----------------------'
M.Tansley 05/99 48311 GREEN 8 40 44
J.LULU 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
----------------------
L.Tansley 05/99 4712 Brown-2 12 30 28
----------------------
Brown 08/99 5877 huangqi 45 22 24
----------------------
L.Tansley 05/99 47120 Brown-2 12 30 28
----------------------
15.将第2行到第6行的小写字母全部转为大写
[root@localhost script]# cat new|sed '2,6y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'
M.Tansley 05/99 48311 GREEN 8 40 44
J.LULU 06/99 48317 GREEN 9 24 26
P.BUNNY 02/99 48 YELLOW 12 35 28
J.TROLL 07/99 4842 BROWN-3 12 26 26
L.TANSLEY 05/99 4712 BROWN-2 12 30 28
BROWN 08/99 5877 HUANGQI 45 22 24
L.Tansley 05/99 47120 Brown-2 12 30 28
16.先删除第1行,再将Brown替换为abc
[root@localhost script]# cat new|sed -n -e '1d' -e 's/Brown/abc/p'
J.Troll 07/99 4842 abc-3 12 26 26
L.Tansley 05/99 4712 abc-2 12 30 28
abc 08/99 5877 huangqi 45 22 24
L.Tansley 05/99 47120 abc-2 12 30 28
阅读(971) | 评论(0) | 转发(0) |