[英]Printing nth rows after a row number in awk
1,A
2,B
3,C
4,D
5,E
6,F
7,G
8,H
9,I
10,J
11,K
12,L
13,M
14,N
How do I print row 4 first and then the 9th row and then the 14th row using awk?如何使用 awk 先打印第 4 行,然后打印第 9 行,然后打印第 14 行? I want to first print the 4th row and then continue printing every 5th row after the 4th row, until the end of the file.我想先打印第 4 行,然后在第 4 行之后每隔 5 行继续打印,直到文件结束。 I tried this:我试过这个:
awk '{if(NR==4 || (NR>4 && NR==NR+7)) print $0}' file
But this doesn't work.但这不起作用。 Any help is appreciated.任何帮助表示赞赏。
The output should be: output 应该是:
4,D
9,I
14,N
Assuming you want to print every 5th line starting from a specific line number:假设您要从特定行号开始每隔 5 行打印一次:
$ seq 20 | awk 'NR==4{c=4} c && !((++c) % 5)'
4
9
14
19
$ seq 20 | awk 'NR==2{c=4} c && !((++c) % 5)'
2
7
12
17
$ seq 20 | awk 'NR==6{c=4} c && !((++c) % 5)'
6
11
16
c && !((++c) % 5)
says: c && !((++c) % 5)
说:
If
c
is set then incrementc
and test if that new value modulo5
is zero.如果c
已设置,则增加c
并测试该新值模5
是否为零。
So no line before NR==4
can be printed as c
is never populated before that happens, and then when c
is set to 4, it's then increment to 5
and 5 % 5
is 0
so the line is printed.因此,在NR==4
之前不能打印任何行,因为c
在此之前从未填充过,然后当c
设置为 4 时,它会递增到5
并且5 % 5
为0
,因此会打印该行。 c
gets incremented for every line after that and so c % 5
continually rotates through 1 2 3 4 0
thus printing every 5th line when 0
occurs and so !0
is true. c
之后的每一行都会递增,因此c % 5
不断旋转1 2 3 4 0
,因此当0
出现时每 5 行打印一次,所以!0
为真。
To do the above using values set on the command line rather than hard-coded in the script would be:使用在命令行上设置的值而不是在脚本中硬编码来执行上述操作将是:
$ seq 20 | awk -v b=4 -v n=5 'NR==b{c=n-1} c && !((++c) % n)'
4
9
14
19
Simply简单地
awk 'NR%5 == 4' file
will do the job.将完成这项工作。 Alternatively, if you have GNU sed
:或者,如果您有 GNU sed
:
sed -n 4~5p file
Your code你的代码
awk '{if(NR==4 || (NR>4 && NR==NR+7)) print $0}' file
contain condition NR==NR+7
which does never hold, in turn what is right to ||
包含条件NR==NR+7
永远不成立,反过来什么是正确的||
is always false and therefore your code is in fact acting like it would be总是错误的,因此您的代码实际上就像它一样
awk '{if(NR==4) print $0}' file
print row 4 first and then the 9th row and then the 14th row using awk?使用 awk 先打印第 4 行,然后打印第 9 行,然后打印第 14 行?
Just provide GNU AWK
with ;
只需提供 GNU AWK
;
-sheared conditions describing lines you want that is - 描述你想要的线条的剪切条件是
awk 'NR==4;NR==9;NR==14' file
Keep in mind that altering order does not change output, so you will get same output for请记住,更改顺序不会更改 output,因此您将获得相同的 output
awk 'NR==14;NR==9;NR==4' file
(tested in GNU Awk 5.0.1) (在 GNU Awk 5.0.1 中测试)
awk
without ever using modulo %
:在awk
中执行此操作,而无需使用modulo %
:jot 30 | mawk 'BEGIN { __+=_+=_+=__=_^=FS="^$" } _-(_+=__*(_==NR))'
4
9
14
19
24
29
generic solution without modulo %
:没有modulo %
通用解决方案:
-- "1st print
17th
row then once every
29
rows after that"
-- "1st print
17th
row then once every
29
rows after that"
jot 200 | gawk '__-(__+=___*(NR == __))' FS='^$' __=17 ___=29
17
46
75
104
133
162
191
jot 14 | nawk '_*=--_'
1
3
5
7
9
11
13
Here is another awk
solution这是另一个awk
解决方案
$ seq 30 | awk -v b=14 -v n=5 'NR>=b && !((NR-b)%n)'
14
19
24
29
this is just the translation of sed -n 14~5p
.这只是sed -n 14~5p
的翻译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.