linux系统sed命令输出匹配字符的行及其后若干行

1、测试数据

[root@PC3 test]# cat c.txt
1
2
3
4
5
6
7
8
9
10

2、提取包含2的行,其后1行,其后2行

[root@PC3 test]# sed -n '/2/p' c.txt
2
[root@PC3 test]# sed -n '/2/,+1p' c.txt
2
3
[root@PC3 test]# sed -n '/2/,+2p' c.txt
2
3
4

3、提取2后面的1行,5后面的2行

[root@PC3 test]# sed -n '/2/,+1p; /5/,+2p' c.txt
2
3
5
6
7
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14344407.html