I have file that looks like:
ATOM 2517 O VAL 160 8.337 12.679 -2.487
ATOM 2518 OXT VAL 160 7.646 12.461 -0.386
TER
ATOM 2519 N VAL 161 -14.431 5.789 -2
use strict;
use warnings;
use Tie::File;
my @array;
tie @array, 'Tie::File', 'myFile.txt' or die "Unable to tie file";
my %unwanted = map { $_ => 1 } # Hashify ...
map { $_-1, $_, $_+2 .. $_+4 } # ... the five lines ...
grep { $array[$_] =~ /^TER/ } # ... around 'TER' ...
0 .. $#array ; # ... in the file
# Remove the unwanted lines
@array = map { $array[$_] } grep { ! $unwanted{$_} } 0 .. $#array;
untie @array; # The end