Perl 遍历文件中的每一行并附加到另一个文件中每一行的末尾 - 跟进
Perl iterating through each line in a file and and appending to the end of each line in another file - follow up
我有一个关于更早 post 的跟进问题。
有问题的 post 是:
Perl iterating through each line in a file and appending to the end of each line in another file
我用过:
use warnings;
use strict;
open my $animals, '<', 'File1.txt' or die "Can't open animals: $!";
open my $payloads, '<', 'File2.txt' or die "Can't open payloads: $!";
my @payloads = <$payloads>; #each line of the file into an array
close $payloads or die "Can't close payloads: $!";
while (my $line = <$animals>) {
chomp $line;
print $line.$_ foreach (@payloads);
}
close $animals or die "Can't close animals: $!";
这适用于看起来像这样的文件:
file 1: file 2:
line1 lineA
line2 lineB
line3 lineC
但不适用于看起来像这样的文件:
<01 line1
<02 line2
所以我想做的是:
file 1: file 2:
<01 line1 <AA lineAA
<02 line2 <AB lineAB
应该变成:
file 3:
<01_AA line1lineAA
<01_AB line1lineAB
<02_AA line2lineAA
<02_AB line2lineAB
我试图通过在 while 循环中使用 while 循环拆分选项卡上的字符串来解决它(见下文),但我无法让它工作。
我的脚本:
#!C:/perl64/bin/perl.exe
use warnings;
use strict;
open my $file1, '<', 'file1.fasta' or die "Can't open file1: $!";
open my $file2, '<', 'file2.fasta' or die "Can't open file2: $!";
open(OUT, '>', 'file3.fasta') or die "Cannot write $!";
while (<$file2>)
{
chomp;
my ($F2_Id, @SF2_seq) = split (/\t/, $_);
while (<$file1>)
{
chomp;
my ($F1_Id, @F1_seq) = split (/\t/, $_);
foreach my $seq (@F1_seq)
{
print OUT $F1_Id,"_",$F2_Id,"\t",$seq.$_ foreach (@F2_seq),"\n";
}
close;
}
}
我最近才开始使用 perl,所以我可以想象脚本中有很多错误。
很抱歉拖了这么久post,但我会感谢任何帮助。
您可以将第一个文件的id和seq存储在一个数组数组中。
您还必须将第二个文件中的 <
替换为 _
。
#!/usr/bin/perl
use warnings;
use strict;
open my $LEFT, '<', 'file1.fasta' or die "Can't open file1: $!";
open my $RIGHT, '<', 'file2.fasta' or die "Can't open file2: $!";
open my $OUT, '>', 'file3.fasta' or die "Cannot write: $!";
my @left;
while (<$LEFT>) {
chomp;
push @left, [ split /\t/ ];
}
while (<$RIGHT>) {
chomp;
my ($id, $seq) = split /\t/;
$id =~ s/</_/;
print {$OUT} "$_->[0]$id\t$_->[1]$seq\n" for @left;
}
close $OUT or die "Cannot close: $!";
我有一个关于更早 post 的跟进问题。 有问题的 post 是: Perl iterating through each line in a file and appending to the end of each line in another file
我用过:
use warnings;
use strict;
open my $animals, '<', 'File1.txt' or die "Can't open animals: $!";
open my $payloads, '<', 'File2.txt' or die "Can't open payloads: $!";
my @payloads = <$payloads>; #each line of the file into an array
close $payloads or die "Can't close payloads: $!";
while (my $line = <$animals>) {
chomp $line;
print $line.$_ foreach (@payloads);
}
close $animals or die "Can't close animals: $!";
这适用于看起来像这样的文件:
file 1: file 2:
line1 lineA
line2 lineB
line3 lineC
但不适用于看起来像这样的文件:
<01 line1
<02 line2
所以我想做的是:
file 1: file 2:
<01 line1 <AA lineAA
<02 line2 <AB lineAB
应该变成:
file 3:
<01_AA line1lineAA
<01_AB line1lineAB
<02_AA line2lineAA
<02_AB line2lineAB
我试图通过在 while 循环中使用 while 循环拆分选项卡上的字符串来解决它(见下文),但我无法让它工作。
我的脚本:
#!C:/perl64/bin/perl.exe
use warnings;
use strict;
open my $file1, '<', 'file1.fasta' or die "Can't open file1: $!";
open my $file2, '<', 'file2.fasta' or die "Can't open file2: $!";
open(OUT, '>', 'file3.fasta') or die "Cannot write $!";
while (<$file2>)
{
chomp;
my ($F2_Id, @SF2_seq) = split (/\t/, $_);
while (<$file1>)
{
chomp;
my ($F1_Id, @F1_seq) = split (/\t/, $_);
foreach my $seq (@F1_seq)
{
print OUT $F1_Id,"_",$F2_Id,"\t",$seq.$_ foreach (@F2_seq),"\n";
}
close;
}
}
我最近才开始使用 perl,所以我可以想象脚本中有很多错误。
很抱歉拖了这么久post,但我会感谢任何帮助。
您可以将第一个文件的id和seq存储在一个数组数组中。
您还必须将第二个文件中的 <
替换为 _
。
#!/usr/bin/perl
use warnings;
use strict;
open my $LEFT, '<', 'file1.fasta' or die "Can't open file1: $!";
open my $RIGHT, '<', 'file2.fasta' or die "Can't open file2: $!";
open my $OUT, '>', 'file3.fasta' or die "Cannot write: $!";
my @left;
while (<$LEFT>) {
chomp;
push @left, [ split /\t/ ];
}
while (<$RIGHT>) {
chomp;
my ($id, $seq) = split /\t/;
$id =~ s/</_/;
print {$OUT} "$_->[0]$id\t$_->[1]$seq\n" for @left;
}
close $OUT or die "Cannot close: $!";