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