简体   繁体   中英

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 :

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

$1="chr1" is actually just assigning chr1 to $1 and it will always return true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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