我有 3 个或多个文件需要合并,数据如下所示..
file 1
0334.45656
0334.45678
0335.67899
file 2
0334.89765
0335.12346
0335.56789
file 3
0334.12345
0335.45678
0335.98764
文件 4 中的预期输出,
0334.89765
0334.89765
0334.89765
0334.12345
0335.67899
0335.12346
0335.56789
0335.45678
0335.98764
到目前为止我已经尝试过,但是第 4r 个文件中的数据没有按排序顺序出现,
#!/usr/bin/perl
my %hash;
my $outFile = "outFile.txt";
foreach $file(@ARGV)
{
print "$file\n";
open (IN, "$file") || die "cannot open file $!";
open (OUT,">>$outFile") || die "cannot open file $!";
while ( <IN> )
{
chomp $_;
($timestamp,$data) = split (/\./,$_);
$hash{$timeStamp}{'data'}=$data;
if (defined $hash{$timeStamp})
{
print "$_\n";
print OUT"$_\n";
}
}
}
close (IN);
close (OUT);
最佳答案
我通常不会建议这样做,但 unix 实用程序应该能够很好地处理它。
cat
将 3 个文件放在一起。- 使用
sort
对合并后的文件进行排序。
但是,使用 perl,可以执行以下操作:
#!/usr/bin/perl
use strict;
use warnings;
my @data;
push @data, $_ while (<>);
# Because the numbers are all equal length, alpha sort will work here
print for sort @data;
但是,正如我们所讨论的,文件可能会非常大。因此,如果您能够利用所有文件都已排序这一事实,那么内存和速度都会更加高效。
因此,以下解决方案对文件进行流式处理,在 while 的每个循环中按顺序拉出下一个文件:
#!/usr/bin/perl
# Could name this catsort.pl
use strict;
use warnings;
use autodie;
# Initialize File handles
my @fhs = map {open my $fh, '<', $_; $fh} @ARGV;
# First Line of each file
my @data = map {scalar <$_>} @fhs;
# Loop while a next line exists
while (@data) {
# Pull out the next entry.
my $index = (sort {$data[$a] cmp $data[$b]} (0..$#data))[0];
print $data[$index];
# Fill In next Data at index.
if (! defined($data[$index] = readline $fhs[$index])) {
# End of that File
splice @fhs, $index, 1;
splice @data, $index, 1;
}
}
关于Perl 合并文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22586089/