Aggregated points

Combine points of specific key (a1) based on user defined size (lets say 200 in this example).
so a1 191 and 191+200 and sum of all the values (4th column)
and vice versa...

Thanx a bunch!

a1 	191 	201 	1
a1 	201 	211 	2
a1 	211 	221 	1
a1	
.......
....
a2.........
.....
aT........
.........

output

a1	191	391	22
a1     391   591   66
..
a2	777	977	12
..
aT	100	300	66	
..

How do you determine the user-defined size, is is passed as a parameter to the program or is it derived from the input?

Can't see any 4th column in the input or output, can you please specify how the 3 values in the output are supposed to be calculated.

  1. it is passed a parameter (200)
    2.output
    1st value =same as input (ex: a1)
    2nd value= first value (ex: a1 191)
    3rd value = second value (191,201,211,...untill it gets 391 (191+200)
    4th value = is the sum of 1+2+1.... (of 191 -201, 201-211....391)

so you need get the value every 200, but if 200 in the middle of line such as:

a1 	389 	395 	1

391 is in the middle, will you calculate 1 with which record? How do you handle it?

Then print that one as it is in output. No need of any modification for such values.

Here is a script that does the job just pass limit and datafile on command line eg:

$ sumdata 200 mydata.dat
#!/bin/ksh
gap=$1
start=0
limit=0
total=0
while read key min max amt
do
   if [ -n "$prev_key" -a "$key" != "$prev_key" ]
   then
       echo $prev_key $start $limit $total
       total=0
       limit=0
   fi
   if [ $limit -gt 0 -a $limit -lt $min ]
   then
       echo $prev_key $start $limit $total
       total=0
       limit=0
   fi
   if [ $limit -eq 0 ]
   then
       start=$min
       let limit=start+gap
    fi
    prev_key=$key
    let total+=amt
done < $2
echo $ckey $start $limit $total
a1 191 391 26
a1 401 601 14
a1 2921 3121 20
a1 4781 4981 39
a1 4991 5191 1
a1 6051 6251 54
a1 6261 6461 73
a1 6471 6671 16

Hey than alot! It is working fine. is it possible to continue the flow 191:391-391-591....?means if it didnt find any thing it should print 0 like [a1 391 591 0].

a1 191 391 26
a1 391 591 0
a1 591 791 ...................

This should work

#!/bin/ksh
gap=$1
start=0
limit=0
total=0
while read key min max amt
do
   if [ "$key" != "$ckey" ]
   then
       [ -n "$ckey" ] && echo $ckey $start $limit $total
       total=0
       start=$min
       let limit=start+gap
   fi
   if [ $limit -lt $min ]
   then
       echo $ckey $start $limit $total
           total=0
           start=$limit
           let limit+=gap
           while [ $start -le $(($min-$gap)) ]
           do
              echo $ckey $start $limit 0
              start=$limit
              let limit+=gap
           done
   fi
   ckey=$key
   let total+=amt
done < $2
echo $ckey $start $limit $total
1 Like