Perl遍历文件中的每一行,并附加到另一个文件中每行的末尾 - 跟进

时间:2015-04-07 10:12:51

标签: perl filehandle

关于更早的帖子,我有一个跟进问题。 该帖子是: 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,所以我可以想象脚本中存在很多错误。

对于很长的帖子我感到很抱歉,但我会饶有兴趣。

1 个答案:

答案 0 :(得分:0)

您可以将第一个文件的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: $!";