perl to remove lines from file

前端 未结 4 1068
春和景丽
春和景丽 2021-01-29 12:22

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         


        
4条回答
  •  暖寄归人
    2021-01-29 13:03

    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
    

提交回复
热议问题