【问题标题】:zcat to read gzip files and then concatenate them in Perlzcat 读取 gzip 文件,然后在 Perl 中连接它们
【发布时间】:2016-03-05 13:47:10
【问题描述】:

我需要编写一个 perl 脚本来从其路径的文本文件列表中读取 gzip 文件,然后将它们连接在一起并输出到一个新的 gzip 文件。 (我需要在 perl 中执行此操作,因为它将在管道中实现) 我不确定如何完成 zcat 和连接部分,因为文件大小以 Gbs 为单位,我还需要注意存储和运行时间。

到目前为止,我可以认为它是 -

use strict;
use warnings;
use IO::Compress::Gzip qw(gzip $GzipError) ;

#-------check the input file specified-------------#

$num_args = $#ARGV + 1;
if ($num_args != 1) {
    print "\nUsage: name.pl Filelist.txt \n";
exit;

$file_list = $ARGV[0];

#-------------Read the file into arrray-------------#

my @fastqc_files;   #Array that contains gzipped files 
use File::Slurp;
my @fastqc_files = $file_list;


#-------use the zcat over the array contents 
my $outputfile = "combined.txt"
open(my $combined_file, '>', $outputfile) or die "Could not open file '$outputfile' $!";

for my $fastqc_file (@fastqc_files) {

    open(IN, sprintf("zcat %s |", $fastqc_file)) 
      or die("Can't open pipe from command 'zcat $fastqc_file' : $!\n");
    while (<IN>) {
        while ( my $line = IN ) {
          print $outputfile $line ;
        }
    }
    close(IN);

my $Final_combied_zip = new IO::Compress::Gzip($combined_file);
  or die "gzip failed: $GzipError\n";

不知何故,我无法让它运行。另外,如果有人可以指导输出此压缩文件的正确方法。

谢谢!

【问题讨论】:

  • 你试过zcat file1 file2 file3 ... filen | gzip &gt; out.gz(未经测试)吗?
  • 你有没有尝试过?因为这样做的一些尝试肯定会得到更好的答案。有一些模块可以轻松做到这一点。或者有一个执行管道的open
  • @Sobrique 我尝试使用 zcat 读取 gzip 文件,但我不确定是否可以简单地将它与从列表中读取的每个 gzip 文件连接起来

标签: perl concatenation gzip zcat


【解决方案1】:

你不需要 perl。你甚至不需要 zcat/gzip,因为 gzip 文件是 catable:

cat $(cat pathfile) >resultfile

但如果你真的需要尝试通过组合来获得额外的压缩:

zcat $(cat pathfile)|gzip >resultfile

添加:还要注意右边的第一个“相关”链接,它似乎已经回答了这个问题:How to concat two or more gzip files/streams

【讨论】:

  • 如果您只是将一堆 gzip 文件合并到一个 gzip 文件中,标题和尾部(CRC32 校验和)会不会混淆?编辑:它会,但 gunzip 仍然能够正确解压缩一个大的 gzip 文件。
  • 不是根据链接问题的链接:The gzip manual says that two gzip files can be concatenated as you attempted.gnu.org/software/gzip/manual/gzip.html#Advanced-usage
  • 我需要在程序中进一步阅读组合文件,该程序会将组合的 zip 文件视为一个文件,所以我无法连接 zip 文件
【解决方案2】:

感谢您的回复 - 脚本现在运行良好 -

#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp;
use IO::Compress::Gzip qw(gzip $GzipError);


my @data = read_file('./File_list.txt');
my $out = "./test.txt";


foreach my $data_file (@data)

{
    chomp($data_file);
    system("zcat $data_file >> $out");
}
my $outzip = "./test.gz";
gzip $out => $outzip;

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 2018-12-19
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多