The sample input file content is: 2012/02/12 @ 14:29:26,519 @ -> java.lang.NullPointerException 2012/02/12 @ 14:23:26,519 @ -> | WARN | RMI TCP Connection(184923)-170.80.0.9 | Error in getting the Network Adapter 2012/02/12 @ 14:20:26,522 @ -> | WARN | RMI TCP Connection(184923)-170.80.0.9 | Error in getting the Network Adapter and output should look like: 2012/02/12 @ 14:20:26,522 @ -> | WARN | RMI TCP Connection(184923)-170.80.0.9 | Error in getting the Network Adapter 2012/02/12 @ 14:23:26,519 @ -> | WARN | RMI TCP Connection(184923)-170.80.0.9 | Error in getting the Network Adapter 2012/02/12 @ 14:29:26,519 @ -> java.lang.NullPointerException #### open FH_duplicate, "$file_duplicate" or die "$!"; open FH1_sorting, ">>$file_consolidated_sort" or die "$!"; my %hash = (); my $key; my $val; while() { chomp; ($key,$val)=split(/,,/); $hash{$key} .= $val; } close FH_duplicate; ### hash creation ### sorting begins for $key(map{$_ -> [0]} sort{ $a->[1] cmp $b->[1] || $a->[2] cmp $b->[2]} map{[$_,(split)[0],(split)[2]]} keys %hash) { print FH1_sorting "$key -> $hash{$key}"; } close FH1_sorting;