How to get n lines for every m lines (n<m) in command-line?

You can use the ~ in the address in GNU sed:

sed -n '1~4p;2~4p'

Which reads "Print the first line every 4 lines, and print the second line every 4 lines" or "Starting from line 1, print every 4th line, and starting from line 2, print every 4th line".


With gnu split:

n=2
m=4 
split -l ${m} --filter="head -n ${n}" infile

and if you wanted to do it only after the ith line, just redirect the previous lines to /dev/null:

n=2
m=4 
i=7
{ head -n ${i} >/dev/null; split -l ${m} --filter="head -n ${n}"; } <infile

If you don't have access to gnu tools you could use awk:

awk -vn=2 -vm=4 -vi=7 'NR<=i{next}; (NR-i)%m==1{c=1}; c++<=n' infile

For GNU sed

sed '3~4,+1 d' file

or more general:

m=4
n=2
sed "$((m-n+1))~$m,+$((m-n-1)) d" file