Linux 中 sed -E选项支持扩展正则表达式

 

001、 情形1

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt        ## 测试数据
01 02 03
04 0ababab5 06
07 08 09
10 11 12
13 14 15
16 1ababab7 18
[root@PC1 test]# sed 's/ab+/QQQ/' a.txt
01 02 03
04 0ababab5 06
07 08 09
10 11 12
13 14 15
16 1ababab7 18
[root@PC1 test]# sed 's/\(ab\)\+/QQQ/' a.txt     ## 将连续的ab替换为特定内容
01 02 03
04 0QQQ5 06
07 08 09
10 11 12
13 14 15
16 1QQQ7 18
[root@PC1 test]# sed -E 's/(ab)+/QQQ/' a.txt     ## -E 选项可以省略转义字符
01 02 03
04 0QQQ5 06
07 08 09
10 11 12
13 14 15
16 1QQQ7 18

image

 

002、情形2

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt
01 02 03
04 05 06
07 08 09
10 11 12
13 14 15
16 17 18
[root@PC1 test]# sed 's/06|15/QQQ/' a.txt
01 02 03
04 05 06
07 08 09
10 11 12
13 14 15
16 17 18
[root@PC1 test]# sed 's/06\|15/QQQ/' a.txt      ## 逻辑或需使用转义字符
01 02 03
04 05 QQQ
07 08 09
10 11 12
13 14 QQQ
16 17 18
[root@PC1 test]# sed -E 's/06|15/QQQ/' a.txt     ## -E 选项支持扩展正则
01 02 03
04 05 QQQ
07 08 09
10 11 12
13 14 QQQ
16 17 18

image

 。

 

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