linux 系統 awk命令提取奇數行、偶數行及整數倍行


1、創建測試數據

[root@linuxprobe test]# seq 20 > 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]# awk 'NR%2 != 0' a.txt 1
3
5
7
9
11
13
15
17
19

 

3、提取偶數行

[root@linuxprobe test]# awk 'NR%2 == 0' a.txt 2
4
6
8
10
12
14
16
18
20

 

4、提取非三倍數行

[root@linuxprobe test]# awk 'NR%3 != 0' a.txt 1
2
4
5
7
8
10
11
13
14
16
17
19
20

 

5、提取三倍數行

[root@linuxprobe test]# awk 'NR%3 == 0' a.txt 3
6
9
12
15
18

其余以此類推。


免責聲明!

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



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