Script that sorts and show only unique IP

Hi,

I have this data:

Jun 16  88.191.117.191  =  100
Jun 16  88.191.117.191  =  70
Jun 16  88.191.117.191  =  30
Jun 16  200.242.67.142  =  65
Jun 16  125.77.107.212  =  40
Jun 16  125.77.107.212  =  60

And I want to have the ff ouput:

Jun 16  88.191.117.191  =  200
Jun 16  125.77.107.212  =  100
Jun 16  200.242.67.142  =  65

It adds the 5th column ( value ) for the same IP address and then sort it.

I've thinking on this since yesterday but cant find the solution.
Can you help me on this ?

$ cat input.txt
Jun 16  88.191.117.191  =  100
Jun 16  88.191.117.191  =  70
Jun 16  88.191.117.191  =  30
Jun 16  200.242.67.142  =  65
Jun 16  125.77.107.212  =  40
Jun 16  125.77.107.212  =  60
$ perl -le 'while($l=<>){@pt=split /=/,$l;$tot{$pt[0]}+=$pt[1];}print join "\n",map {$_."  =  ".$tot{$_}}keys%tot;' input.txt
Jun 16  88.191.117.191    =  200
Jun 16  125.77.107.212    =  100
Jun 16  200.242.67.142    =  65

Thanks Pludi,

your script is in perl. do you have an equivalent bash shell script on this ?
sorry.. my existing script is on bash scripting.

I think its easy for me to understand if its using bash scripting.

thanks in advance

This one may be easier to understand... though I didn't implement the sorting step:

$ perl -ane 'if ($a == 0) { push @x, @F; $a=1; next; }
             if ($a == 1 && $F[2] eq $x[2]) { $x[4] = $x[4] + $F[4]; }
             else { print "@x\n"; @x=(); push @x, @F; }
             END
             { print "@x\n"; }' inputfile
Jun 16 88.191.117.191 = 200
Jun 16 200.242.67.142 = 65
Jun 16 125.77.107.212 = 100
$ 

Hi
Try this:

$ awk '{_[$1" "$2" "$3]+=$5;}END{for (i in _) print i, " = ",_;}' file
Jun 16 125.77.107.212  =  100
Jun 16 200.242.67.142  =  65
Jun 16 88.191.117.191  =  200

Guru.

Thanks to all..

I love this forum :slight_smile:

This one sorts as well:

awk '{v=$5;$5="";A[$0]+=v}END{for (i in A) print i A}' infile | sort -ut. -k3,6rn
Jun 16 88.191.117.191 = 200
Jun 16 125.77.107.212 = 100
Jun 16 200.242.67.142 = 65