Angelo
Angelo

Reputation: 5059

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

Upvotes: 1

Views: 59

Answers (1)

anubhava
anubhava

Reputation: 784998

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.

Upvotes: 7

Related Questions