linux 中sed命令删除匹配字符及其后的若干行

发布时间 2023-09-25 10:18:14作者: 小鲨鱼2018

 

001、方法1

(base) [root@pc1 test1]# ls
a.txt
(base) [root@pc1 test1]# cat a.txt
01 02 03
04 05 06
07 08 09
10 11 12
13 14 15
keyword
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
(base) [root@pc1 test1]# sed '/keyword/{N;N;d}' a.txt
01 02 03
04 05 06
07 08 09
10 11 12
13 14 15
22 23 24
25 26 27
28 29 30

 

002、方法2

(base) [root@pc1 test1]# ls
a.txt
(base) [root@pc1 test1]# cat a.txt
01 02 03
04 05 06
07 08 09
10 11 12
13 14 15
keyword
16 17 18
19 20 21
22 23 24
25 26 27
28 29 30
(base) [root@pc1 test1]# sed '/keyword/,+2d' a.txt
01 02 03
04 05 06
07 08 09
10 11 12
13 14 15
22 23 24
25 26 27
28 29 30

 。