简体   繁体   English

使用awk对最后一列中的值求和

[英]summing up the values in last column using awk

I have a tab delimited file of five columns and it looks like this: 我有一个由五列组成的制表符分隔文件,它看起来像这样:

chr1    0   210515280   249250621   0.844593
chr1    1   15116879    249250621   0.0606493
chr1    2   9272046 249250621   0.0371997
chr1    3   5395181 249250621   0.0216456
chr1    4   3089690 249250621   0.0123959
chr1    5   1767270 249250621   0.00709033
chr1    6   1001501 249250621   0.00401805
chr1    7   590059  249250621   0.00236733
chr1    8   367487  249250621   0.00147437
chr1    9   247265  249250621   0.000992034

I want to add the values in last column and print it. 我想在最后一列中添加值并打印出来。

What I am doing: 我在做什么:

awk '{if($1="chr1") total += $5 } END { print total }'<file

and its not working. 它不起作用。

Any suggestions 有什么建议么

Thank you 谢谢

You need to use == for comparison and no need to do use <file : 您需要使用==进行比较,而无需使用<file

awk '$1 == "chr1" { total += $5 } END { print total }' file

$1="chr1" is actually just assigning chr1 to $1 and it will always return true. $1="chr1"实际上只是将chr1分配给$1 ,它将始终返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM