sed学习-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 3977327
  • 博文数量: 536
  • 博客积分: 10470
  • 博客等级: 上将
  • 技术积分: 4825
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-26 14:08
文章分类

全部博文(536)

文章存档

2024年(3)

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(252)

2006年(73)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: linux

2008-10-11 22:25:03

测试的环境:
[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
凯发app官方网站 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在字符替换部分写的可能还不是很全面,慢慢添加吧。。。
阅读(4133) | 评论(0) | 转发(0) |
0

上一篇:linux cut命令

下一篇:awk --- print学习

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图