awk script to add numbers in one column -


how can add (numbers on 2nd column) using for or while loops in awk script?

while numbers @ $1 random , in increasing order:

  • 1 through 2 in first iteration,
  • 1.1 through 2.1 in 2nd iteration,
  • 1.2 through 2.2 in 3rd iteration,
  • 1.3 through 2.3 in 4th iteration
  • ... end

that means 0.1 increment @ each iteration.

expected output:

  • 39 1st iteration
  • 47 2nd iteration...

input data:

1.0  1 1.1  3 1.2  4 1.3  3 1.4  5 1.5  7 1.6  10 2.0  6 2.1  9 2.2  2 2.3  8 2.4  0 3.0  4 3.2  5 4.0  8 4.1  6 5.0  7 6.0  6 7.0  7 8.7  9 9.8  2 

here, multiply each $1 10 avoid problems imprecise decimal numbers.

awk -v max=$(tail -1 data | awk '{print $1*10}') '     {n = $1 * 10}     nr==1 {min = n}     {          (i=min; i<=(max-10); i++) {             if (i <= n && n <= (i+10)) {                 sum[i, i+10] += $2             }         }     }     end {         (key in sum) {             split(key, a, subsep)             printf "[%.1f,%.1f] = %d\n", a[1]/10, a[2]/10, sum[key]         }     } ' data | sort -n 

output

[1.0,2.0] = 39 [1.1,2.1] = 47 [1.2,2.2] = 46 [1.3,2.3] = 50 ... [8.6,9.6] = 9 [8.7,9.7] = 9 [8.8,9.8] = 2 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -