使用第二列的内容替换一列中的NAN或零

时间:2013-10-22 12:34:26

标签: perl

我希望file1.txt中第2列(当NAN / 0时)的内容被column1的内容替换:

这是我的输入file1.txt:

 file for parsing
 mnot   NAN
 PU1     0
 PU2     ets
 munt    tsu
 PU3    ttsm
 munt2    0

这是必需的输出文件:

file for parsing
mnot   mnot
PU1    PU1
PU2    ets
munt   tsu
PU3    ttsm    
munt2  munt2

我的代码(如下)没有给出正确的输出:

#!usr/bin/perl
use warnings;
use strict;
use diagnostics;

open(IN, "<", "file1.txt") or die "Can't open file for reading:$!";

my $header = <IN>;
print OUT $header;


while (<IN>){
chomp;
my @sections = split(/\t/);
$sections[0] = 0;
$sections[1] = 0;

if (($sections[1] eq 'NAN') || ($sections[1] == 0)) {
    print OUT $sections[0], "\t", $sections[1], "\n";
    #print OUT "$sections[0]\n";
    }   
else {
    print OUT $sections[0], "\t", $sections[1], "\n";
    #print OUT "$sections[2]\n";
    }
 }

请帮忙!

1 个答案:

答案 0 :(得分:1)

$.是当前行号,因此标题为$. == 1

时 连接双引号内的元素时,

$"是数组分隔符,即。 "@sections"

use warnings;
use strict;

open(my $IN, "<", "file1.txt") or die "Can't open file for reading:$!";
open(my $OUT, ">", "outfile.txt") or die "Can't open file for writing:$!";

local $" = "\t";
while (my $line = <$IN>) {
  chomp $line;
  my @sections = split(/\t/, $line);

  if ((!$sections[1] or $sections[1] eq 'NAN') and $. > 1) {
    $sections[1] = $sections[0];
    print $OUT "@sections\n";
    next;
  }
  print $OUT "$line\n";
}