Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk columnwise sum a particular group

I am new to shell scripting.I have a CSV file and i want to print column wise sum of rows that match some condition

column1   column2   column3   column4 column5.......    columnN
a1         b1        c1       0.5      50                100

a2         b2        c2       1       25                150

a1         b1        c2       0.5      25                 10

a2         b2        c2      2        20                100   

(assuming comma as separator i can SUM a particular column by

 awk -F ',' '{ x = x + $4 } END { print x }'

1) How can i use it in a loop from kth to Nth to sum columns with numeric values?

2)Also how can i use it conditionally only to sum columns of rows that match some condition say,column2 is b2 and column3 is c2? I can do

  cat file| grep b2 |grep c2| awk...as answer to 1st question

but it would be very naive .Can you please help me with above two queries?

Based on answers below i tried

awk -F’,' -v k=“3" -v n=“6" '$2=="b2" && $3="c2"{for(i=k;i<=n;i++)s[i]+=$i} 
        END{for(x in s)printf "sum of column %d is %s\n",x,s[x]}’ test.csv

but it gives syntax error

 -bash: syntax error near unexpected token `i=k'
like image 617
bl3e Avatar asked Nov 20 '25 03:11

bl3e


1 Answers

you can combine two conditions: ($2 == "b2" && $3 == "c2") and (from kth - nth columns)

awk -F'whatever' -v k="$k" -v n="$n" 
                '$2=="b2" && $3="c2"{for(i=k;i<=n;i++)s[i]+=$i}
                END{for(x in s)printf "sum of column %d is %s\n", x,s[x]}' file

in above codes:

  • -F'whatever' is the column separator
  • -v k="$k" -v n="$n" k and n are the column range. You can hard code them or use shell variables: $k/$n
  • output will be something like:

    sum of column 3 is 300
    sum of column 4 is 400
    
  • and the codes were not tested

like image 159
Kent Avatar answered Nov 21 '25 17:11

Kent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!