我有多个文本文件。我已经编写代码来通过shell输入2个文件并合并它们。但我如何合并多个文件。我的系统命令有用于此目的。perl代码合并多个文本文件
my @a = read_file($file1)
or die "couldn't read $file1 - $!";
my @b = read_file($file2)
or die "couldn't read $file2 - $!";
my $combined = {}; # hashref
my $i=0;
foreach (@a) {
chomp;
$combined->{$i}{b} = '' unless defined $combined->{$i}{b};
$combined->{$i++}{a} = $_;
}
$i=0;
foreach (@b) {
chomp;
$combined->{$i}{a} = '' unless defined $combined->{$i}{a};
$combined->{$i++}{b} = $_;
}
foreach my $i (sort {$a<=>$b} keys %$combined) {
print $combined->{$i}{a}, ("\t" x 2), $combined->{$i}{b}, "\n";
}
这不会以OP想要的方式合并文件,它会连接它们。 – TLP 2012-07-05 09:13:41