Linux中实现每隔指定行规律输出文本内容

发布时间 2023-10-12 14:35:23作者: 小鲨鱼2018

 

001、间隔2

[root@pc1 test1]# ls
a.txt
[root@pc1 test1]# cat a.txt        ## 测试数据
1
2
3
4
5
6
7
8
9
10
[root@pc1 test1]# awk '{if(NR % 2 == 1) {count++}; if(NR % 2 == 1 && count % 2 != 0) {row = NR + 2}; if(NR < row) {print $0}}' a.txt
1
2
5
6
9
10

 

002、间隔3

[root@pc1 test1]# ls
a.txt
[root@pc1 test1]# cat a.txt       ## 测试数据
1
2
3
4
5
6
7
8
9
10
[root@pc1 test1]# awk '{if(NR % 3 == 1) {count++}; if(NR % 3 == 1 && count % 2 != 0) {row = NR + 3}; if(NR < row) {print $0}}' a.txt
1                                   ## 间隔3
2
3
7
8
9

 

003、间隔4

[root@pc1 test1]# ls
a.txt
[root@pc1 test1]# cat a.txt        ## 测试数据
1
2
3
4
5
6
7
8
9
10
[root@pc1 test1]# awk '{if(NR % 4 == 1) {count++}; if(NR % 4 == 1 && count % 2 != 0) {row = NR + 4}; if(NR < row) {print $0}}' a.txt
1                                ## 间隔4
2
3
4
9
10

 

004、从第二个分割开始,间隔2

[root@pc1 test1]# ls
a.txt
[root@pc1 test1]# cat a.txt       ## 测试数据
1
2
3
4
5
6
7
8
9
10
[root@pc1 test1]# awk '{if(NR % 2 == 1) {count++}; if(NR % 2 == 1 && count % 2 == 0) {row = NR + 2}; if(NR < row) {print $0}}' a.txt
3                      ## 间隔2
4
7
8

 

005、从第二个间隔开始,间隔3

[root@pc1 test1]# ls
a.txt
[root@pc1 test1]# cat a.txt      ## 测试数据
1
2
3
4
5
6
7
8
9
10
[root@pc1 test1]# awk '{if(NR % 3 == 1) {count++}; if(NR % 3 == 1 && count % 2 == 0) {row = NR + 3}; if(NR < row) {print $0}}' a.txt
4                       ## 间隔3
5
6
10

 。