Previously defined solutions are really complicated! All you need is
s/^(?:.*\n){1,3}//; s/(?:.*\n){1,3}\z//;
If you want to accept the flaw of the trailing newline at the end, you can use
s/\n?\z/\n/; s/^(?:.*\n){1,3}//; s/(?:.*\n){1,3}\z//;
or
s/^(?:.*\n){1,3}//; s/(?:.*\n){0,2}.*\n?\z//;
source share