#TopAndTail.pl #Removes header and footer lines from text files. #John Nurick 2004-05 use strict; die "\nTopAndTail.pl: removes header and footer lines from text files. \n\n" . "Usage: perl TopAndTail.pl filespec hdrlines ftrlines [-b]\n\n" . " filespec can be a wildcard \n" . " hdrlines and ftrlines are number of lines to omit.\n" . " -b is optional switch to leave original file with .BAK extension.\n\n" . "NB: slurps each file into memory so not suitable for very large files." unless defined $ARGV[2]; my ($hdrlines, $ftrlines) = @ARGV[1 .. 2]; my $iofile; #path and name of input file my $bakname; #path and name for backup file undef $/; #slurp entire file my @files = glob($ARGV[0]); #treat first argument as a wildcard foreach $iofile (@files) { open IOFILE, $iofile or die "Couldn't open $iofile for input.\n"; my (@lines) = split "\n", ; if ($#lines < $hdrlines + $ftrlines) { warn "$iofile doesn't have enough lines!\n"; close IOFILE; next; } close IOFILE; if ($ARGV[3] =~ m(^-b)i ) { # -b switch $bakname = $iofile; $bakname =~ s/\.[^\.]*$/\.BAK/; unlink $bakname; #delete old .BAK file if it exists rename $iofile, $bakname; } pop @lines until $lines[-1]; #dump any empty line(s) due to \n at end of infile open IOFILE, "> $iofile" or die "Couldn't open $iofile for output.\n"; print IOFILE join "\n", @lines[$hdrlines .. $#lines - $ftrlines]; close IOFILE; print STDOUT "$iofile\n"; #list files as they are processed }