每 3 列获取所有行的平均值

每 3 列获取所有行的平均值

我使用的是 Ubuntu 14.04。给定制表符分隔的文件:

示例 字段 Field2 Field3 Field4 Field5 Field6 Field7 Field8 Field9 Field10 Field11 Field12

样品1 1 2 3 4 5 6 7 8 9 10 11 12

我想打印所有行的列的平均值(每列 3 列),输出如下:

示例字段 Field2 Field3 Field4

样本 2 5 8 11

提前致谢!

答案1

我会像这样解决它:

#!/usr/bin/perl

use warnings;
use strict;

my $field_count = 3;

#discard first row, as the fields don't match
my $first_row = <>;
#iterate STDIN or files specified on command line, just like grep or sed do. 
while ( <> ) {
   #extract the name and values. Maybe you need a 'chomp' to remove linefeeds 
   #it works given your sample data, because the last field is a number. 
   my ( $samplename, @fields ) = split; 
   my @new_fields; 
   while ( @fields ) {
      #extract fields 3 at a time.  
      my @group = splice @fields, 0, $field_count;
      #sum them
      my $sum = 0;
      $sum += $_ for @group;

      my $avg = $sum / @group; #divide by number of elements in this group, so it'll work if there's 1 or 2 'trailing'. 
      #stash that in the new field list. 
      push @new_fields, $avg;
   }
   #print the output line. 
   print join "\t", $samplename, @new_fields,"\n"
}

答案2

A1 Perl:对所有行使用传统

假设输入格式:sampleId,3 个值组

perl -nE '($out,@g)=split;                   #sampleId a1 b1 c1  a2 b2 c2 ...
          while(($a,$b,$c,@g)=@g){           
             $out .= " ".($a+$b+$c)/3 }
          say $out '

或者

A2 Perl:仅使用正则表达式、替换和评估

perl -pe 's!\b(\d+)\s+(\d+)\s+(\d+)! ($1+$2+$3)/3 !ge'

相关内容