#!/usr/bin/perl -Tw use strict; use warnings; my $skiphead = 1; my $skipfoot = 2; my $infile = 'test.txt'; my $outfile = '/tmp/out.txt'; my @buffer = (); open IN, '<', $infile or die "Cannot open $infile for reading: $!"; open OUT, '>', $outfile or die "Cannot open $outfile for writing: $!"; for (1..$skiphead); push @buffer, scalar for (1..$skipfoot); while () { print OUT shift @buffer; push @buffer, $_; } close OUT; close IN; #### $ cat test.txt 1 hello 2 there 3 this 4 is 5 a 6 good 7 test $ ./headtail.pl $ cat out.txt 2 there 3 this 4 is 5 a