#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub fileParse { my ( @files ) = @_; my $refHash = {}; open(my $fh, '<' , $files[0]) or die "Could not open file '$files[0]' $!"; my $pdus = do { local $/; <$fh> }; close $fh or die "Could not close file '$files[0]' $!"; chomp( $pdus ); # my @matches = $pdus =~ /\{ (?: (?R) | [^{}]+ )+ \}/gx; my @matches = $pdus =~ /[^{}]+ | \{ (?: (?R) | [^{}]+ )+ \} /gx; # Trim leading and trailing white spaces s/^\s+|\s+$//g for (@matches); %{$refHash} = @matches; return $refHash; } my $final = fileParse( @ARGV ); my @sorted = (); push @sorted, $_ for (sort keys %{$final}); print Dumper \@sorted; __END__ $VAR1 = [ '[ 11] 25/2/2017-19:02:06.980 proces_name thanos-Rx: UDT', '[ 12] 25/2/2017-19:02:06.996 proces_name thanos-Tx: UDT', '[ 13] 25/2/2017-19:02:07.185 proces_name thanos-Rx: UDT', '[ 14] 25/2/2017-19:02:07.227 proces_name thanos-Tx: UDT', '[ 17] 25/2/2017-19:02:07.413 proces_name thanos-Tx: UDT', '[ 18] 25/2/2017-19:02:09.794 proces_name thanos-Rx: UDT' ];