你觉得这个怎么样?
1:从文件中获取内容(可能使用 File::Slurp 的 read_file)并保存到标量。
use File::Slurp qw(read_file write_file);
my $contents = read_file($filename);
2:有一个与此类似的正则表达式模式匹配:
my @file_rows = ($contents ~= /(AA\s[A-Z]{3}\s+\d+\s+\w*)/);
3:如果第 2 列的值在整个文件中始终是唯一的:
foreach my $file_row (@file_rows) {
my @values = split(' ', $file_row, 3);
write_file($values[1] . ".txt", $file_row);
}
3:否则:拆分行值。使用第二列作为键将它们存储到散列中。使用哈希将数据写入输出文件。
my %hash;
foreach my $file_row (@file_rows) {
my @values = split(' ', $file_row, 3);
if (defined $hash{$value[1]}) {
$hash{$values[1]} .= $file_row;
} else {
$hash{$values[1]} = $file_row;
}
}
foreach my $key (keys %hash) {
write_file($key .'txt', $hash{$key});
}