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 ……

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM