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

…………

 

相关文章:

  • 2022-01-12
  • 2021-08-01
  • 2022-12-23
  • 2022-01-19
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
猜你喜欢
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
  • 2022-01-07
  • 2022-01-07
  • 2022-12-23
相关资源
相似解决方案