How to sum only specific column values in a row using awk?
I have a file which looks like this
Code:
Ref1 BC1:10 BC2:25 BC3:2 BC4:5 BC5:2 PL:2 PL:2 ML:3 ML:2
Ref2 BC2:10 BC6:25 BC9:2 PL:2 PL:1 ML:3 ML:3
Ref3 BC5:2 PL:2 PL:20 ML:30 MLb:3
Ref4 BC7:10 BC8:24 BC3:20 BC4:3 BC5:3 PL:2 PL:20
I want to sum the values associated with "BC" tag and print the number of fields that has BC tag ignoring PL and ML tag values.
I tried using the following command to sum only the values associated with BC tag, but didn't give me the expected results.
Code:
awk 'FS=":"{sum=0;nf=0;for(i=1;i<=NF;i++)if($i~/BC/)nf++;{sum+=$i}; print sum,nf,$0}' File
My result file should look something like this
Code:
44 5 Ref1 BC1:10 BC2:25 BC3:2 BC4:5 BC5:2 PL:2 PL:2 ML:3 ML:2
37 3 Ref2 BC2:10 BC6:25 BC9:2 PL:2 PL:1 ML:3 ML:3
2 1 Ref3 BC5:2 PL:2 PL:20 ML:30 MLb:3
60 4 Ref4 BC7:10 BC8:24 BC3:20 BC4:3 BC5:3 PL:2 PL:20
But at the moment using the above command my results look like the one given below. The number of fields with BC tag is counted correctly but the sum value associated with BC tag is incorrect.
Code:
0 5 Ref1 BC1:10 BC2:25 BC3:2 BC4:5 BC5:2 PL:2 PL:2 ML:3 ML:2
0 3 Ref2 BC2:10 BC6:25 BC9:2 PL:2 PL:1 ML:3 ML:3
0 1 Ref3 BC5:2 PL:2 PL:20 ML:30 MLb:3
0 5 Ref4 BC7:10 BC8:24 BC3:20 BC4:3 BC5:3 PL:2 PL:20
Any ideas of how to get the sum value for only specific fields in a row?
Thanks in advance
|