Help with sum of data set

Input file

2         1159,310,           
4         142,199,218,91,     
3         91,273,349,          

Desired output result

2        1469
4        650
3        713

I have long list of input file as shown above.
It has a "," delimited to separate between each record in column 2.
I would like to sum all the record in same line at column 2 to get the desired output result.
The theory behind to manual calculate to get the Desired output result

2        1159+310=1469
4        142+199+218+91=650
3        91+273+349=713   

Thanks for any advice.

perl -lane '$s=0;$s+=$_ for split /,/,$F[1];print "$F[0] $s"' file

With awk (with some assumptions):

awk -F'[ \t]+|,' '{s=0;for(i=2;i<NF;i++) s+=$i;print $1,s}' file
1 Like