Average from values of fields
Hi,
I have a file with something as follows: 192.168.136.246 10 23 1 10.128.255.158 2 4 9 192.168.134.206 5 7 1 10.128.255.158 3 7 10 and so on... The first field can repeat. I'd want to make an average of each field per IP address, For example for 10.128.255.158 would output 10.128.255.158 2.5 5.5 9.5 Please could you help me to make a script with awk or perl? Thanks in advance! |
What have you got so far?
I've edited my response after I'd slightly misread the complexity. Ok, to average we need either all the values for a specific averaging to be stored before we calculate the average (e.g. for 192.168.136.246's first column we store [10, 5]), or we do a rolling average calculation of the current average and how many values were used to calculate that figure (e.g. [7.5, 2]). So you need a mapping per IP address, which maps to either:
or:
|
Yes awk could easily do this. What have you tried and where are you getting stuck?
|
Since the OP has been quiet, here is an example awk script.
Code:
awk '(NF>=4) { n[$1]++ The END rule will be processed after all the records (lines) have been processed. ip will loop through all keys in the n array, therefore through all IP addresses. Since the v1, v2 and v3 arrays count the sums of the fields, dividing by the number of summands will yield the average. I added the number of occurrences at the end of the line in parenthesis for illustration. You can format the fields (e.g. %.3f instead of just %f to your needs, too). |
All times are GMT -5. The time now is 02:34 AM. |