LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to sum only specific column values in a row using awk? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-sum-only-specific-column-values-in-a-row-using-awk-4175446035/)

jv61 01-17-2013 09:45 AM

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

grail 01-17-2013 10:14 AM

Maybe something like:
Code:

awk -F'[ :]*' '{for(i=2;i< NF;i+=2)if($i ~ /BC/){cnt++;sum+=$(i+1)}$0=sum" "cnt" "$0;sum=cnt=0}1' file

colucix 01-17-2013 10:57 AM

Another one:
Code:

BEGIN {

  RS = "[[:space:]]+"
  FS = ":"
 
}

/^Ref/ {

  if ( n )
    printf "%4d %3d %s\n", sum, n, line
 
  n = 0
  sum = 0
  line = sprintf("%-6s",$0)
 
}

! /^Ref/ {

  line = sprintf("%s %-8s",line,$0)
 
}
 
/BC/ {

  n++
  sum += $2
 
}

END {

  printf "%4d %3d %s\n", sum, n, line
 
}


jv61 01-17-2013 11:41 AM

Thanks for the answers, problem solved


All times are GMT -5. The time now is 10:15 PM.