#!/usr/bin/perl # http://perlmonks.org/?node_id=1196143 use strict; use warnings; $_ = join '', sort ; 1 while s/^(\d+,\d+,)\K (\S+)\n\1(\S+)/ $2 + $3 /emx; print; __DATA__ 3211111,100,3.2 3211112,101,3.2 3211111,100,1.2 3211112,100,2.2 3211113,100,5.2 3211112,100,0.3