根据我的理解,做了一个脚本,在我的机器上试过了:
use 5.016;
use warnings;
use utf8;
use autodie;
my %target;
my $source_file = 'original.txt';
my $target_file = 'target.txt';
my $source_file_fh; # your source file handle
my $target_file_fh; # your target file handle
my $key; # key item in target file
my $content; # last content item in target file
sub get_last_item {
my $str = shift;
$str =~ /.*[ ]+(.*)/;
return $1;
}
sub get_key {
my $str = shift;
my $content = shift;
$str =~ /.*[ ]+(.*)[ ]+$content/;
return $1;
}
open($source_file_fh, "<", $source_file);
open($target_file_fh , ">", $target_file);
while (<$source_file_fh>) {
$content = get_last_item($_);
$key = get_key($_, $content);
$target{$key} = $content if ($key);
}
for (sort keys %target) {
say $target_file_fh "$_ $target{$_}";
}
close $target_file_fh;
close $source_file_fh;