$ perl -MData::Dumper -F, -lane '@F % 2 and ($k)=@F or push @{$D{$k}}, @F; }{ $D{$_} = {@{$D{$_}}} for keys %D; print Dumper \%D' input.txt