linux系统中sed命令整行替换


1、测试数据

[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s

 

2、将第2行替换为xxxx

[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s
[root@centos79 test]# sed '2c xxxx' a.txt 3 4 5
xxxx
s g 8
k s g
2 5 d
s c w
a r t
e 4 s

 

3、将2-5行替换为xxxx

[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s
[root@centos79 test]# sed '2,5c xxxx' a.txt 3 4 5
xxxx
s c w
a r t
e 4 s

 

4、将第2行和第5行替换为xxxx

[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s
[root@centos79 test]# sed -e '2c xxxx' -e '5c xxxx' a.txt 3 4 5
xxxx
s g 8
k s g
xxxx
s c w
a r t
e 4 s

 

5、将奇数行替换为xxxx

[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s
[root@centos79 test]# sed '1~2c xxxx' a.txt
xxxx
d g 3
xxxx
k s g
xxxx
s c w
xxxx
e 4 s

 

正则表达式:……

[root@centos79 test]# cat a.txt
3 4 5
d g 3
s g 8
k s g
2 5 d
s c w
a r t
e 4 s
[root@centos79 test]# sed '/^s/c xxxx' a.txt 3 4 5
d g 3
xxxx
k s g
2 5 d
xxxx
a r t
e 4 s

…………

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM