Sed script that removes specific lines in `seq` output
N;/7/!{P;D}


Nice job there, this very concisely (and clearly) allows you to capture both lines \$n\$ and \$n+1\$.

:b N;s/\n/&amp;/3;Tb;d


Now the loop that follows it is mostly redundant. You are essentially appending lines of input until you are left with \$4\$ lines in total. You already have \$2\$ in your pattern space and need \$2\$ more, i.e. lines \$n-1\$ and \$n-2\$, which can simply be expressed in N;N followed by a d.