Please use ***
[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do
not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.
It would be nice if you showed us that you are attempting to solve it yourself. What have you come up with so far? Have you checked out any of the links I gave you?
In any case, what we need to do here is create an array entry for each company prefix you encounter, and add the
$2 call time to its total each time you encounter it. Then at the end we print out the results.
Now, you haven't explained exactly what part of the number is company prefix, but I assume it's the first 7 digits. So we'll use the
substr function to extract it.
Also, your calculations are a bit more complex than they seem at first glance. It appears that each call has to be calculated separately, and any fractions of a minute have to be counted as a full minute.
Code:
awk '{ comp[substr($3,1,7)] += ( int( $2/60 ) + $2%60 ? 1 : 0 ) } END{ for (i in comp){ printf "%s: %d min\n" , i , comp[i] } }'
The first time
awk finds a company number in
$3, a new entry for it will be added to the
comp array, and its value set to the call total. Every subsequent time the same company matches, it will have that
$2 value added to this.
The call total is calculated by first dividing
$2 by 60, and taking the integer value only. Then a "
%" (modulo operator) calculation is done on the same number to give us the remainder, and a
ternary conditional operator is used to decide whether to add one or zero to it.
Finally, in the END section we simply loop over the array and print the totals.
Note that array loops generally come out unsorted, so you'll have to pipe the output through
sort to clean up the order if you need it. Or if you're using
gawk then you can use one of it's
built-in sorting features.