using perl, how to select last 2 lines in the case of each row has same word?

前端 未结 2 2000
深忆病人
深忆病人 2021-01-29 11:45
Bini  --  -21.89753  -20.47853  -20.27835  -18.34952  -16.23454

Bini  --  -16.89753  -14.47853  -13.27835  -12.34952  -11.23454

Bini  --  -10.09014  

2条回答
  •  灰色年华
    2021-01-29 12:11

    You can take all numbers from every line, push them at the end of @entries, and always keep only last three.

    my @entries;
    while(my $line = ) {
         next if $line !~ /Bini/;
         push @entries, grep /\d/, split /\s+/,$line;
         @entries = @entries[-3 .. -1] if @entries > 3;
    }
    print join "\n", @entries;
    

    output

    -12.34952
    -11.23454
    -10.09014
    

提交回复
热议问题