对列应用计算(awk)

时间:2017-04-03 13:56:45

标签: awk

我尝试将此计算从第41列到行尾:

    awk '{ { split($10,a,":") } { split( a[4], b ,",") } {print b[1]+b[2]}}' filename

我知道如何只在一列上执行此操作,但是当我尝试执行循环时,它会失败:

     awk '{for (i=10;i<=NF;i++) {split($i,a,":")} {split(a[4],b,",")} {print ( b[1]+b[2])}}' filename

目的是分割每一列并对这些数字求和:

  

./:0:,,。。的 0,0 :0,0

以下是我的文件:

Contig  POS  ID  REF  ALT  QUAL  FILTER  INFO  FORMAT  S155  S158  S168  S173  S175  S178  S180  S188  S189  S191  S193  S194  S196  S201  S205  S206  S208  S209  S210
NODE_14985_length_2800_cov_1.38384 67 999978 A C . PASS Ty=SNP;Rk=1;UL=19;UR=31;CL=.;CR=.;Genome=A;Sd=1 GT:DP:PL:AD:HQ ./.:8:.,.,.:8,0:71,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0    ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0    ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0     ./.:0:.,.,.:0,0:0,0

这是我的实际输出:

awk '{for (i=10;i<=NF;i++) {split($i,a,":")} {split(a[4],b,",")} {print b[1]+b[2]}}' file.vcf | head

0
0
0
0
0

我想为每列提供一个计算矩阵:

0 0 0 0 
1 2 0 6
2 0 0 8 
...

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

  • 更改了printf,最后添加了一个打印件(至少需要一个打印空间来分隔行的结果)
  • 根据您的示例将41更改为低于28的数字(此数据集中只有28个awk字段)
  • 你的不同分裂是在循环之后,它们必须在循环范围内(参见括号的位置)

修改后的代码:

awk 'NR > 1 {
   for( i=41; i<=NF; i++) {
      split( $i, a, ":" )
       #print NF ":" i "[" $i "]  a[4]:" a[4]
      split( a[4], b, ",")
       #print i ": " b[1] " + " b[2] " : " b[1] + b[2]
      printf( "%d ",  b[1] + b[2])
      }
   print ""
   }' YourFile