Obtém a média de todas as linhas a cada 3 colunas

1

Estou no Ubuntu 14.04. Dado o arquivo delimitado por tabulação:

Samples Field Field2 Field3 Field4 Field5 Field6 Field7 Field8 Field9 Field10 Field11 Field12

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

Eu quero imprimir a média das colunas (cada 3) para todas as linhas, exibindo algo assim:

Sample Field Field2 Field3 Field4

Sample 2 5 8 11

Obrigado antecipadamente!

    
por Guest321 09.02.2017 / 14:49

2 respostas

1

Eu lidaria com algo assim:

#!/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"
}
    
por 09.02.2017 / 15:02
0

A1 Perl: usando um tradicional para todas as linhas

Assumindo o formato de entrada: sampleId, grupos de 3 valores

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 '

ou

A2 Perl: usando apenas expressões regulares, substitua e eval

perl -pe 's!\b(\d+)\s+(\d+)\s+(\d+)! ($1+$2+$3)/3 !ge'
    
por 09.02.2017 / 17:53