linux 中sed命令删除匹配字符之后、之前的若干行

 

001、 删除匹配字符之后的若干行

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt             ## 测试数据
1
2
3
4
5
6
7
8
9
10
[root@PC1 test]# sed '/5/,+1{/5/b;d}' a.txt       ## 删除匹配5之后的1行
1
2
3
4
5
7
8
9
10
[root@PC1 test]# sed '/5/,+3{/5/b;d}' a.txt       ## 删除匹配5之后的3行
1
2
3
4
5
9
10

 

 

002、删除匹配字符之前的若干行

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt
1
2
3
4
5
6
7
8
9
10
[root@PC1 test]# tac a.txt | sed '/5/,+1{/5/b;d}' | tac        ## 删除匹配5之前的1行
1
2
3
5
6
7
8
9
10
[root@PC1 test]# tac a.txt | sed '/5/,+3{/5/b;d}' | tac       ## 删除匹配5之前的3行
1
5
6
7
8
9
10

 

003、

(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/,+2{/keyword/b;d}' a.txt
01 02 03
04 05 06
07 08 09
10 11 12
13 14 15
keyword
22 23 24
25 26 27
28 29 30

 。

 

posted @ 2022-12-08 20:02  小鲨鱼2018  阅读(1773)  评论(0编辑  收藏  举报