$ perl -F, -lane 'push@{$h{$F[0]}},$F[3]}{$"=",";print"$_,@{[reverse@{$h{$_}}]}"for sort keys%h' < input.txt Kim,on,off Rada,one,zero Tyra,on,off