Linux 中sed命令的整行替换

 

Linux 中sed命令的整行替换.

 

001、基本用法

[root@PC1 test2]# ls
a.txt
[root@PC1 test2]# cat a.txt         ## 测试文件
a       UU      i
a       UU      i
b       q       j
c       q       y
[root@PC1 test2]# sed '/b/ s/.*/QQ/' a.txt     ## 将匹配b的行全部替换为QQ
a       UU      i
a       UU      i
QQ
c       q       y
[root@PC1 test2]# sed '/b/ c\QQ' a.txt          ## 利用c参数选项
a       UU      i
a       UU      i
QQ
c       q       y

 。

 

002、取反

[root@PC1 test2]# ls
a.txt
[root@PC1 test2]# cat a.txt                        ## 测试数据
a       UU      i
a       UU      i
b       q       j
c       q       y
[root@PC1 test2]# sed '/b/! s/.*/QQ/' a.txt         ## 取反1
QQ
QQ
b       q       j
QQ
[root@PC1 test2]# sed '/b/! c\QQ' a.txt              ## 取反方法2
QQ
QQ
b       q       j
QQ

 。

 

posted @ 2025-02-24 17:08  小鲨鱼2018  阅读(120)  评论(0)    收藏  举报