Perl 合并文件 [英] Perl Merge file

查看:84
本文介绍了Perl 合并文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个或多个文件需要合并,数据如下所示..

I have 3 or multiple files I need to merge, the data looks like this..

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 中的预期输出,

Expected output in file 4,

0334.89765
0334.89765
0334.89765
0334.12345
0335.67899
0335.12346
0335.56789
0335.45678
0335.98764

到目前为止,我已经尝试过,但是第 4 个文件中的数据没有按顺序排列,

So far I have tried but data in 4rth file does not come in sorted order,

#!/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 实用程序应该能够很好地处理这个问题.

I wouldn't normally suggest this, but unix utilties should be able to handle this just fine.

  1. cat 将 3 个文件放在一起.
  2. 使用 sort 对合并后的文件进行排序.
  1. cat the 3 files together.
  2. use sort to sort the merged file.

但是,使用 perl,可以只执行以下操作:

However, using perl, could just do the following:

#!/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;

但是,正如我们所讨论的,文件可能会非常大.因此,如果您能够利用所有文件都已排序的事实,那么在内存和速度方面都会更加高效.

However, as we've discussed, it's possible that the files will be extremely large. Therefore it will be more efficient both in memory and speed if you're able to take advantage of the fact that all the files are already sorted.

因此,以下解决方案流式传输文件,在 while 的每个循环中按顺序拉出下一个:

The following solution therefore streams the files, pulling out the next one in order each loop of the 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 合并文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆