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

时间:2015-03-07 03:39:12

标签: 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);

4 个答案:

答案 0 :(得分:2)

获取请求输出的另一种方法是使用一个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

答案 1 :(得分:1)

这样做:

$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,你的请求输出就在那里。

答案 2 :(得分:1)

您的代码以串行方式打印,但您需要并行

#!/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);

答案 3 :(得分:1)

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

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