How can I get a range of line every nth interval using awk, sed, or other unix command? How can I get a range of line every nth interval using awk, sed, or other unix command? unix unix

How can I get a range of line every nth interval using awk, sed, or other unix command?


Using GNU sed:

sed -n '0~17800{N;N;p}' input

Meaning,

For every 17800th line: 0~17800  Read two lines: {N;N;  And print these out: p}

We can also add the first three lines:

sed -n -e '1,3p' -e '0~17800{N;N;p}' input

Using Awk, this would be simpler:

awk 'NR%17800<3 || NR==3 {print}' input


$ cat file12345678910$ awk '!(NR%3)' file369$ awk -v intvl=3 -v delta=2 '!(NR%intvl){print "-----"; c=delta} c&&c--' file-----34-----67-----910$ awk -v intvl=4 -v delta=2 '!(NR%intvl){print "-----"; c=delta} c&&c--' file-----45-----89$ awk -v intvl=4 -v delta=3 '!(NR%intvl){print "-----"; c=delta} c&&c--' file-----456-----8910