简体   繁体   English

如何使用 Perl 合并多个文件?

[英]How merge multiple files using Perl?

I'm trying to merge 3 different files in only one.我试图将 3 个不同的文件合并到一个文件中。 These files have 3 columns, the first 2 columns are the same between the three files.这些文件有 3 列,前 2 列在三个文件之间是相同的。

The files (3 columns * 263.900 rows):文件(3 列 * 263.900 行):

FILE 1
ID_1        ID_2        dN
QJY77946    NP_073551   0.0293
QJY77954    NP_073551   0.0273
QJY77954    QJY77946    0.0041
QJY77962    NP_073551   0.0273
QJY77962    QJY77946    0.0041   
...
FILE 2
ID_1        ID_2        dS
QJY77946    NP_073551   0.0757
QJY77954    NP_073551   0.0745
QJY77954    QJY77946    0.0085
QJY77962    NP_073551   0.0745
QJY77962    QJY77946    0.0109
...
FILE 3
ID_1        ID_2        Omega (dN/dS)
QJY77946    NP_073551   0.3872
QJY77954    NP_073551   0.3668
QJY77954    QJY77946    0.4851
QJY77962    NP_073551   0.3668
QJY77962    QJY77946    0.3767
...

For doing this, I'm using a Perl script to merge the files.为此,我使用 Perl 脚本来合并文件。

This script works when I merge two files (and create one file with 4 columns) but if I try to add one more, doesn't work!当我合并两个文件(并创建一个包含 4 列的文件)时,此脚本有效,但如果我尝试再添加一个,则不起作用!

The script:剧本:

use strict;
use warnings;

# Variables
my $file01 = "M8_S.dNdS.tsv" ;
my $file02 = "M8_dN-dS.tsv" ;
# Define hash
my %content02 = () ;
open (B, $file02) or die;
while (my $l1 = <B>) {
  $l1 =~ s/\n//g;
  $l1 =~ s/\r//g;
  my @cont_linea = split ("\t", $l1);
  my $valor_1 = $cont_linea[0];
  my $valor_2 = $cont_linea[1];
  my $valor_3 = $cont_linea[2];
  my $ids = "$valor_1\_$valor_2";
  $content02{$ids} = $valor_3;  ### <- STORE DATA IN HASH
}
close B;

#### Open second file
open (A, $file01) or die;
LOOPFILE:
while (my $l2 = <A>) {
  $l2 =~ s/\n//g;
  $l2 =~ s/\r//g;
  my @cont_linea_2 = split ("\t", $l2);
  my $valor_21 = $cont_linea_2[0];
  my $valor_22 = $cont_linea_2[1];
  my $valor_23 = $cont_linea_2[2];
  my $ids_2 = "$valor_21\_$valor_22";
  if (exists $content02{$ids_2}) {
    my $newval = $content02{$ids_2};
    print "$l2\t$newval\n";
    next LOOPFILE;
  }
}
close A;
exit;

Any suggestion of what I have to do or add in the script to merge the 3 files and generate something like this:关于我必须做什么或在脚本中添加以合并 3 个文件并生成如下内容的任何建议:

ID_1        ID_2        dN      dS      Omega 
QJY77946    NP_073551   0.0293  0.0757  0.3872
QJY77954    NP_073551   0.0273  0.0745  0.3668
QJY77954    QJY77946    0.0041  0.0085  0.4851
QJY77962    NP_073551   0.0273  0.0745  0.3668
QJY77962    QJY77946    0.0041  0.0109  0.3767 
...

I abstracted the loading to a subroutine so the code is not repeated.我将加载抽象为子程序,因此代码不再重复。 The $phase parameter tells it whether it should initialise the hash, accumulate the columns, or even print the result. $phase 参数告诉它是否应该初始化 hash,累加列,甚至打印结果。 Works with 3 files or more.适用于 3 个或更多文件。

#! /usr/bin/perl
use strict;
use warnings;
use feature qw{ say };

sub load {
    my ($file, $table, $phase) = @_;
    open my $in, '<', $file or die "$file: $!";
    while (<$in>) {
        chomp;
        my @columns = split /\t/;
        my $id = join '_', @columns[0, 1];
        die "Duplicate $id."
            if 'first' eq $phase && exists $table->{$id};

        push @{ $table->{$id} }, $columns[2];
        say join "\t", @columns[0, 1], @{ $table->{$id} }
            if 'print' eq $phase;
    }
}

my %table;
my $phase = 'first';
while (my $file = shift @ARGV) {
    load($file, \%table, $phase);
    $phase = 1 == @ARGV ? 'print' : '';
}

Use cut and paste in the shell instead of Perl:在 shell 而不是 Perl 中使用cutpaste

paste file1.tsv <(cut -f3 file2.tsv) <(cut -f3 file3.tsv) > out.tsv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM