如何使用 Perl 合并多个文件?
How merge multiple files using Perl?
我正在尝试将 3 个不同的文件合并到一个文件中。这些文件有3列,三个文件的前2列相同。
文件(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
...
为此,我使用 Perl 脚本来合并文件。
此脚本在我合并两个文件(并创建一个包含 4 列的文件)时有效,但如果我尝试再添加一个,则不起作用!
脚本:
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;
关于我必须做什么或在脚本中添加以合并 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
...
我将加载抽象为一个子程序,所以代码不再重复。 $phase 参数告诉它是否应该初始化散列、累积列,甚至打印结果。适用于 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' : '';
}
在 shell 中使用 cut
and paste
而不是 Perl:
paste file1.tsv <(cut -f3 file2.tsv) <(cut -f3 file3.tsv) > out.tsv
我正在尝试将 3 个不同的文件合并到一个文件中。这些文件有3列,三个文件的前2列相同。
文件(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
...
为此,我使用 Perl 脚本来合并文件。
此脚本在我合并两个文件(并创建一个包含 4 列的文件)时有效,但如果我尝试再添加一个,则不起作用!
脚本:
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;
关于我必须做什么或在脚本中添加以合并 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
...
我将加载抽象为一个子程序,所以代码不再重复。 $phase 参数告诉它是否应该初始化散列、累积列,甚至打印结果。适用于 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' : '';
}
在 shell 中使用 cut
and paste
而不是 Perl:
paste file1.tsv <(cut -f3 file2.tsv) <(cut -f3 file3.tsv) > out.tsv