菜鸟笔记
提升您的技术认知

linux 系统sed命令如何提取奇数行、偶数行及整倍数行

1、创建测试数据 

[root@linuxprobe test]# seq 30 > a.txt [root@linuxprobe test]# ls a.txt [root@linuxprobe test]# head a.txt 1
2
3
4
5
6
7
8
9
10

 

 

2、提取奇数行 

[root@linuxprobe test]# sed -n '1~2p' a.txt 1
3
5
7
9
11
13
15
17
19
21
23
25
27
29

 

3、提取偶数行  

[root@linuxprobe test]# sed -n '0~2p' a.txt 2
4
6
8
10
12
14
16
18
20
22
24
26
28
30

 

4、提取三倍数行

[root@linuxprobe test]# sed -n '0~3p' a.txt 3
6
9
12
15
18
21
24
27
30

其余以此类推。

 

5、每行加倍

[root@linuxprobe test]# sed '1~1p' a.txt 1
1
2
2
3
3
4
4
5
5
6
6
7
7 ……