perl -F"\s+" -lane "$sec?(push $h{$F[0]},@F[2,4]):($h{$F[3]}=[@F[0..2]]);$sec++ if eof;END{print map{qq($_ = @{$h{$_}}\n)}sort keys %h}" mergedhash01.txt mergedhash02.txt 3162 = 501093 0 0 501093 0 3163 = 2958 0 0 2958 0 3164 = 1895 0 0 1895 0 3165 = 1382 0 0 1382 0 3166 = 2869 0 0 2869 0