Andrea Francia Andrea Francia - 3 months ago 7
Bash Question

How to write a unix filter that outputs only a line every N lines

Suppose to feed the filter standard input with these line:

line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10


It would be nicer if someone tell me how to write a script that prints only every 4 lines, in the case of the example input above:

line 1
line 5
line 9

mat mat
Answer
$ yes | cat -n | head -10 | awk 'NR % 4 == 1'
     1  y
     5  y
     9  y

That is, your answer is "awk 'NR % 4 == 1'".