Michael Alipio wrote:
Hi,

I have a long log file that looks like this:

7491    | 210.23.185.123   | PH    202597706
7491    | 210.23.169.91    | PH    202594221
7303    | 201.252.130.245  | AR    201955854
9318    | 210.205.6.225    | KR    201892149
9930    | 210.19.229.57    | MY    201418551
9600    | 210.251.253.180  | JP    201362230
9929    | 210.82.176.84    | CN    201069109
7491    | 210.23.182.102   | PH    201006342
9600    | 210.251.253.180  | JP    201322301
4134    | 58.215.76.36     | CN    170125144
4844    | 210.23.5.177     | SG    170067928

It contains 4 columns, 1st and 2nd as well as 2nd and 3rd were separated by a
pipe, My goal is to add up the last column belonging to the same Country (column 3) display it in descending order, such that I may know what country received the biggest amount of bytes. The countries included in the log file are not a fixed list. Sometimes it contains PH, JP, KR sometimes, it contains
PH and JP only, and sometimes it contains even more countries CN, JP, DE,
US, PH, JP, KR.
>
It might look like this:


Country         Total Bytes

PH              4000000000
KR              3000000000
JP              2000000000
CN              1000000000
...
...

And I'm thinking, perhaps I should get a unique list of all countries
included in this logfile and only then I can aggregate their Bytes by using
regexp.

Can you give me some hints on how I can accomplish this goal.

Hello Michael. How about the program below?

HTH,

Rob



use strict;
use warnings;

my %total;
while (<DATA>) {
  next unless /([A-Z]{2})\s+(\d+)/;
  $total{$1} += $2;
}

foreach (sort { $total{$b} <=> $total{$a} } keys %total) {
  printf "%s -> %d\n", $_, $total{$_};
}

__DATA__
7491    | 210.23.185.123   | PH    202597706
7491    | 210.23.169.91    | PH    202594221
7303    | 201.252.130.245  | AR    201955854
9318    | 210.205.6.225    | KR    201892149
9930    | 210.19.229.57    | MY    201418551
9600    | 210.251.253.180  | JP    201362230
9929    | 210.82.176.84    | CN    201069109
7491    | 210.23.182.102   | PH    201006342
9600    | 210.251.253.180  | JP    201322301
4134    | 58.215.76.36     | CN    170125144
4844    | 210.23.5.177     | SG    170067928

**OUTPUT**

PH -> 606198269
JP -> 402684531
CN -> 371194253
AR -> 201955854
KR -> 201892149
MY -> 201418551
SG -> 170067928

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to