perl -ne '($.<6 and print) or shift @a; push @a,$_; END {print @a}' file