Sed Skills

测试的环境:
[service@gan .ssh]$ uname -a
Linux gan 2.4.21-20.EL #1 SMP Wed Aug 18 20:34:58 EDT 2004 x86_64 x86_64 x86_64 GNU/Linux

[service@gan .ssh]$ sed -V
GNU sed version 4.0.7
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.

文章出处:http://gan.cublog.cn

1>. 删除普通字符操作
[service@gan .ssh]$ cat dfile.txt
LINE1
LINE2
LINE3
LINE4
LINE5
LINE6
LINE7

删除指定行2
[service@gan .ssh]$ sed ‘2d’ dfile.txt
LINE1
LINE3
LINE4
LINE5
LINE6
LINE7
http://gan.cublog.cn
删除1到5行数据
[service@gan .ssh]$ sed ‘1,5d’ dfile.txt
LINE6
LINE7

删除除1到4行的所有行
[service@gan .ssh]$ sed ‘1,4 !d’ dfile.txt
LINE1
LINE2
LINE3
LINE4

删除1行,3行数据
[service@gan .ssh]$ sed -e ‘1d’ -e ‘3d’ dfile.txt
LINE2
LINE4
LINE5
LINE6
LINE7

删除最后一行数据
[service@gan .ssh]$ sed ‘$d’ dfile.txt
LINE1
LINE2
LINE3
LINE4
LINE5
LINE6
http://gan.cublog.cn
删除3到最后一行
[service@gan .ssh]$ sed ‘3,$d’ dfile.txt
LINE1
LINE2

删除包含4字符的行
[service@gan .ssh]$ sed ‘/4/’d dfile.txt
LINE1
LINE2
LINE3
LINE5
LINE6
LINE7
[service@gan .ssh]$ sed -e ‘/4/d’ dfile.txt
LINE1
LINE2
LINE3
LINE5
LINE6
LINE7

———————
[service@gan .ssh]$ cat dfile.txt
LINE1
LINE2
LINE3http://gan.cublog.cn
LINE4
LINE5
LINE6
LINE7
ABC123
123XXABC
W00–

删除包含4,5字符的行
[service@gan .ssh]$ sed ‘/4/,/5/’d dfile.txt #这种做法不建议使用,因为有时候会不行
LINE1
LINE2
LINE3
LINE6
LINE7
ABC123
123XXABC
W00–
[service@gan .ssh]$ sed ‘/4/d;/5/d’ dfile.txt
LINE1
LINE2
LINE3
LINE6
LINE7
ABC123
123XXABC
W00–
http://gan.cublog.cn
删除开头字符为A和包含5字符的行
[service@gan .ssh]$ sed -e “/^A/d;/5/d” dfile.txt
LINE1
LINE2
LINE3
LINE4
LINE6
LINE7
123XXABC
W00–

这样操作产生的结果不知道是什么,很奇怪的结果
[service@gan .ssh]$ sed -e “/^A/,/5/d” dfile.txt
LINE1
LINE2
LINE3
LINE4
LINE5
LINE6
LINE7

删除空行(用上正侧表达式了)
[service@gan .ssh]$ sed -e ‘/^$/d’ dfile.txt

2>. 特殊字符的删除
[service@gan .ssh]$ cat sp.txt
A”/`!B\C’*<>&%#@~()?{}|+_-=:;K 1
A/`!B\C’*<>&%#@~()?{}|+_-=:;K 2
A`!B\C’*<>&%#@~()?{}|+_-=:;K 3
A!B\C’*<>&%#@~()?{}|+_-=:;K 4
AB\C’*<>&%#@~()?{}|+_-=:;K 5
ABC’*<>&%#@~()?{}|+_-=:;K 6
ABC*<>&%#@~()?{}|+_-=:;K 7
ABC<>&%#@~()?{}|+_-=:;K 8
ABC>&%#@~()?{}|+_-=:;K 9
ABC&%#@~()?{}|+_-=:;K 10
ABC%#@~()?{}|+_-=:;K 11
ABC#@~()?{}|+_-=:;K 12
ABC@~()?{}|+_-=:;K 13
ABC~()?{}|+_-=:;K 14
ABC()?{}|+_-=:;K 15
ABC)?{}|+_-=:;K 16
ABC?{}|+_-=:;K 17
ABC{}|+_-=:;K 18
ABC}|+_-=:;K 19
ABC|+_-=:;K 20
ABC+_-=:;K 21
ABC_-=:;K 22
ABC-=:;K 23
ABC=:;K 24
ABC:;K 25
ABC;K 26
ABCK 27

[service@gan .ssh]$ sed ‘/”/d’ sp.txt
A/`!B\C’*<>&%#@~()?{}|+_-=:;K 2
…… 其他的行省略不写了

[service@gan .ssh]$ sed ‘/\//d’ sp.txt
A`!B\C’*<>&%#@~()?{}|+_-=:;K 3
…… 采用转义字符\
http://gan.cublog.cn
[service@gan .ssh]$ sed ‘/`/d’ sp.txt
A!B\C’*<>&%#@~()?{}|+_-=:;K 4
……
[service@gan .ssh]$ sed ‘/!/d’ sp.txt
AB\C’*<>&%#@~()?{}|+_-=:;K 5
……
[service@gan .ssh]$ sed ‘/\\/d’ sp.txt
ABC’*<>&%#@~()?{}|+_-=:;K 6
…… 采用转义字符\
[service@gan .ssh]$ sed “/’/d” sp.txt
ABC*<>&%#@~()?{}|+_-=:;K 7
…… 使用””符号
[service@gan .ssh]$ sed ‘/*/d’ sp.txt
ABC<>&%#@~()?{}|+_-=:;K 8
…… 后面的其他字符(<>&%#@~()?{}|+_-=:;)同普通字符删除操作一样

对于sed在字符删除部分写的可能还不是很全面,慢慢添加吧。。。