views:

492

answers:

3

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
+12  A: 
$ yes | cat -n | head -10 | awk 'NR % 4 == 1'
     1  y
     5  y
     9  y

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

mat
It's refreshing that someone is aware of the default action in awk.
PEZ
+1  A: 
awk '{ if ((NR-1) %4 ==0) print}'
Stephen Darlington
+1  A: 
awk 'NR%4 == 1 {print}'</etc/hosts

Replace 4 by whatever value you want of course.

Keltia