繁体   English   中英

如何使用Perl合并两个文件的某些列?

[英]How can I merge some columns of two files using perl?

我想合并input1.txt的第一列和input2.txt的第三列。 我该怎么做? 我的代码无法满足我的要求。

输入1:

1 6
2 7
3 8
4 9

输入2:

a 4 8
b 6 7
c 3 4
d 2 6

要求的输出:

1 8
2 7
3 4
4 6

我的代码:

#!/usr/bin/perl 

use strict;
use warnings;

open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $!};

open my $input2, '<', "input2.txt" or die qq{Failed to open "input2.txt" for writing: $!};

open my $outfile, '>', "outfile.txt" or die qq{Failed to open "outfile.txt" for writing: $!};


while(<$input1>)
  { 
  my @columns1 = split;
  print $outfile join("\t", $columns1[0], "\n");
  }

while(<$input2>)
  { 
   my @columns2 = split;
   print $outfile join("\t", $columns2[2], "\n");
  }
close($input1);
close($input2);
close($outfile);

获得请求的输出的另一种方法是使用一个while循环而不是两个:

mod.pl

#!/usr/bin/perl 

use strict;
use warnings;

open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $!};

open my $input2, '<', "input2.txt" or die qq{Failed to open "input2.txt" for writing: $!};

open my $outfile, '>', "outfile.txt" or die qq{Failed to open "outfile.txt" for writing: $!};


while(my $l1 = <$input1>){ 
 my $l2 = <$input2>;
 chomp $l1;
 chomp $l2;
  my @columns1 = split(/ /, $l1);
  my @columns2 = split(/ /, $l2);
  print $outfile join("\t", $columns1[1-1], $columns2[3-1]),"\n";
}

  close($input1);
  close($input2);
  close($outfile);

#$ perl mod.pl 
#$ cat outfile.txt 
1   8
2   7
3   4
4   6

做这个:

$filename1 = $ARGV[0]; #for taking input1.txt as the first argument
$filename2 = $ARGV[1]; #for taking input2.txt as the second argument
    @data1;
    @column1;
    open(INPUT_FILE, $filename1)
        or die "Couldn't open $filename1!";
    while (<INPUT_FILE>) { 

        my $currentLine = $_; #read the input file one line at a time, storing it to $currentLine
        @data1  = split " ", $currentLine; #split your line by space
        $firstcolumn = $data1[0]; #store the first column's data 
        push @column1, $firstcolumn ; #push the first column's data into an array

    }

    @data2;
    @column3;
    open(INPUT_FILE, $filename2)
        or die "Couldn't open $filename2!";
    while (<INPUT_FILE>) {

        my $currentLine = $_;       
        @data2  = split " ", $currentLine;
        $thirdcolumn = $data2[2]; #store the third column's data 
        push @column3, $thirdcolumn ;
    }
    $size = @column1;
    open (OUTPUTFILE, '>>outfile.txt'); 

    for($i = 0; $i < $size; $i++){
      print OUTPUTFILE "$column1[$i] $column3[$i]\n"; #writing each entry into the outfile.txt
    }

    close(INPUT_FILE);
    close (OUTPUTFILE);

在命令行中运行perl程序时,请执行以下操作:

yourprogram.pl input1.txt input2.txt outfile.txt

它应该工作。

我尝试了该程序并打开了outfile.txt,您的请求输出就在其中。

您的代码是串行打印的,但是您需要并行打印

#!/usr/bin/perl 

use strict;
use warnings;

open my $input1, '<', "input1.txt" or die qq{Failed to open "input1.txt" for writing: $!};

open my $input2, '<', "input2.txt" or die qq{Failed to open "input2.txt" for writing: $!};

open my $outfile, '>', "outfile.txt" or die qq{Failed to open "outfile.txt" for writing: $!};

my ($line1, $line2);
while(1)
  { 
    $line1 = <$input1> || '';
    $line2 = <$input2> || '';
  my @columns1 = split ' ', $line1;
  my @columns2 = split ' ', $line2;

  print $outfile join("\t", $columns1[0], $columns2[2]), "\n";
  last if !$line1 && !$line2;
  }
close($input1);
close($input2);
close($outfile);

不必这么复杂。 读取数组中第一个文件的第一列,并将其与第二个文件的第三个字段一起打印。 除非文件的行数不同,否则应该可以正常工作。

perl -lane'
    BEGIN { $x = pop; @col1 = map { (split)[0] } <>; @ARGV = $x } 
    print join " ", $col1[$.-1], $F[-1]
' input1 input2
1 8
2 7
3 4
4 6

暂无
暂无

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

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