0

我希望将 file1.txt 中第 2 列(当 NAN/0)的内容替换为第 1 列的内容:

这是我的输入 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";
    }
 }

请帮忙!

4

1 回答 1

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";
}
于 2013-10-22T12:44:03.187 回答