测试的环境:
[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
gan.cublog.cn
[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.
1>. 替换普通字符
[service@gan .ssh]$ cat file.txt
1A1A
2B2B
3C3C
gan.cublog.cn
默认就替换第一个字符
[service@gan .ssh]$ sed 's/1/X/' file.txt
XA1A
2B2B
3C3C
指定只替换第2个字符
[service@gan .ssh]$ sed 's/1/X/2' file.txt
1AXA
2B2B
3C3C
替换所有字符
[service@gan .ssh]$ sed 's/1/X/g' file.txt
XAXA
2B2B
3C3C
2>. 替换特殊字符
注意:替换中sed使用的符号全部是""符号不是''的
[service@gan .ssh]$ cat sp.txt
A"/`!B\C'*<>&%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/\"/X/" sp.txt
AX/`!B\C'*<>&%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/\//X/" sp.txt
A"X`!B\C'*<>&%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/\`/X/" sp.txt
A"/X!B\C'*<>&%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/\!/X/" sp.txt
A"/`XB\C'*<>&%#@~()?{}|+_-=:;K
gan.cublog.cn
[service@gan .ssh]$ sed "s/\\\/X/" sp.txt
A"/`!BXC'*<>&%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/'/X/" sp.txt
A"/`!B\CX*<>&%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/*/X/" sp.txt
A"/`!B\C'X<>&%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/
A"/`!B\C'*X>&%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/>/X/" sp.txt
A"/`!B\C'*<X&%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/&/X/" sp.txt
A"/`!B\C'*<>X%#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/%/X/" sp.txt
A"/`!B\C'*<>&X#@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/#/X/" sp.txt
A"/`!B\C'*<>&%X@~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/@/X/" sp.txt
A"/`!B\C'*<>&%#X~()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/~/X/" sp.txt
A"/`!B\C'*<>&%#@X()?{}|+_-=:;K
[service@gan .ssh]$ sed "s/(/X/" sp.txt
A"/`!B\C'*<>&%#@~X)?{}|+_-=:;K
[service@gan .ssh]$ sed "s/)/X/" sp.txt
A"/`!B\C'*<>&%#@~(X?{}|+_-=:;K
[service@gan .ssh]$ sed "s/?/X/" sp.txt
A"/`!B\C'*<>&%#@~()X{}|+_-=:;K
[service@gan .ssh]$ sed "s/{/X/" sp.txt
A"/`!B\C'*<>&%#@~()?X}|+_-=:;K
[service@gan .ssh]$ sed "s/}/X/" sp.txt
A"/`!B\C'*<>&%#@~()?{X|+_-=:;K
[service@gan .ssh]$ sed "s/|/X/" sp.txt
A"/`!B\C'*<>&%#@~()?{}X+_-=:;K
[service@gan .ssh]$ sed "s/+/X/" sp.txt
A"/`!B\C'*<>&%#@~()?{}|X_-=:;K
[service@gan .ssh]$ sed "s/_/X/" sp.txt
A"/`!B\C'*<>&%#@~()?{}|+X-=:;K
[service@gan .ssh]$ sed "s/-/X/" sp.txt
A"/`!B\C'*<>&%#@~()?{}|+_X=:;K
[service@gan .ssh]$ sed "s/=/X/" sp.txt
A"/`!B\C'*<>&%#@~()?{}|+_-X:;K
[service@gan .ssh]$ sed "s/:/X/" sp.txt
A"/`!B\C'*<>&%#@~()?{}|+_-=X;K
[service@gan .ssh]$ sed "s/;/X/" sp.txt
A"/`!B\C'*<>&%#@~()?{}|+_-=:XK
gan.cublog.cn
3>. 对应多个替换
[service@gan .ssh]$ cat file.txt
1A1AA1
2B2B
3C3C
12345678
23456789
[service@gan .ssh]$ sed 's/123/ABC/' file.txt
1A1AA1
2B2B
3C3C
ABC45678
23456789
gan.cublog.cn
对应的一次全部替换:1->A,2->B,3->C
[service@gan .ssh]$ sed 'y/123/ABC/' file.txt
AAAAAA
BBBB
CCCC
ABC45678
BC456789
4>. 在sed替换中使用正侧表达式
要了解下面的东西最好先了解正侧表达式的,该功能可以让sed功能大大增强。
下面的替换我全部采用替换成X字符吧,当然如果你需要替换成空那就把X去掉就可以了(起到删除字符的作用,其实它还是一个字符替换的)
[service@gan .ssh]$ cat file.txt
1A1AA1
2B2B
3C3C D
12345678 M
23456789
在每行的开头插入X字符
[service@gan .ssh]$ sed 's/^/X/' file.txt
X1A1AA1
X2B2B
X 3C3C D
X 12345678 M
X23456789
在每行的结尾插入X字符
[service@gan .ssh]$ sed 's/$/X/' file.txt
1A1AA1X
2B2BX
3C3C DX
12345678 MX
23456789X
将每行的开头的空格替换成X
[service@gan .ssh]$ sed 's/^ /X/' file.txt
1A1AA1
2B2B
X3C3C D
X 12345678 M
23456789
将每行开头的多个(一个或0个)空格替换成一个X字符
[service@gan .ssh]$ sed 's/^ */X/' file.txt
X1A1AA1
X2B2B
X3C3C D
X12345678 M
X23456789
[service@gan .ssh]$ sed 's/^[ ]*/X/' file.txt
X1A1AA1
X2B2B
X3C3C D
X12345678 M
X23456789
将每行包含有空格的字符替换成X
[service@gan .ssh]$ sed 's/^[ ][ ]*/X/' file.txt
1A1AA1
2B2B
X3C3C D
X12345678 M
23456789
------------------
[gan@localhost tmp]$ cat file.txt
LINE1 Date
LINE2 abc
LINE3 7823
LINE4 s0df
LINE5 8&32*
LINE6 ~!@#$%^*((
引用前面的配比字符串
[gan@localhost tmp]$ sed 's/LINE/& name/' file.txt
LINE name1 Date
LINE name2 abc
LINE name3 7823
LINE name4 s0df
LINE name5 8&32*
LINE name6 ~!@#$%^*((
[gan@localhost tmp]$ sed 's/LINE/LINE name/' file.txt
LINE name1 Date
LINE name2 abc
LINE name3 7823
LINE name4 s0df
LINE name5 8&32*
LINE name6 ~!@#$%^*((
-------------------
用sed替换功能来判断一个变量是否为数值:
[gan@localhost tmp]$ cat num.sh
a=12345.67x
b=`echo $a |sed 's/[0-9]//g;s/\.//;s/-//'`
[ -z "$b" ] && echo "$a is number" || echo "$a is not a number"
ref:
对于sed在字符替换部分写的可能还不是很全面,慢慢添加吧。。。
阅读(4152) | 评论(0) | 转发(0) |