msk_0984 has asked for the wisdom of the Perl Monks concerning the following question:
This one is mine and I am sorry for my last question. Any ways I am greping out filenames from a particular directory and displaying all the file contents which are nothing but log file. But my file names are being created day wise ... Because of which the filenames are not getting sorted and it is displaying the logs in a incorrect order.
Example: (filenames) webadmin_jul_10_2007.log webadmin_jul_11_2007.log webadmin_jul_12_2007.log webadmin_jul_13_2007.log webadmin_jul_14_2007.log webadmin_jul_7_2007.log webadmin_jul_8_2007.log webadmin_jul_9_2007.log
Output :#### upper code ......... still present ##### my $dir = './logs/commonlogs'; opendir my $dh, $dir or die "Can't opendir '$dir': $!\n"; my @files = grep { ! -d "$dir/$_" and ! /user.log/ } readdir $dh; foreach my $file ( @files ) { ## .....Some code written for filtering .. open(FH, $file ) or die "Error : $! \n" ; while($audit_data=<FH>) { @check=split(']\[|]\s+|^\[',$audit_data); print " $check[2] $check[3] $check[1] $check[4] \n"; ### } }
Fri Jul 13 00:25:16 2007 india01 Syslog_Probe Properties file backup taken successfully
Fri Jul 13 00:25:16 2007 india01 ObjectServer Successfully Changed to MSK_P
Sat Jul 7 14:33:02 2007 india01 Stopping the PAD
Sat Jul 7 14:33:10 2007 india01 Starting the PAD
Sat Jul 9 15:17:51 2007 station18 Install login failed
Sat Jul 10 10:17:51 2007 station18 Install Interface entry
Sat Jul 11 00:33:02 2007 india01 Stopping the PAD
The output is coming in an incorrect order because the filenames grep out are coming in an incorrect order. So wat I did was sorted that array
So the output is still not in sorted order it is taking the elements as strings even though i gave it to sort in numerical way. So how to sort that array in keep that array in sorted order according to dates as@files_sorted = sort { $a <=> $b } @files;
Thanks in advance ..Required output --> <p><p> webadmin_jul_7_2007.log webadmin_jul_8_2007.log webadmin_jul_9_2007.log webadmin_jul_10_2007.log webadmin_jul_11_2007.log webadmin_jul_12_2007.log webadmin_jul_13_2007.log webadmin_jul_14_2007.log <p>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sort an array which contains date formatted elements
by moritz (Cardinal) on Jul 17, 2007 at 12:35 UTC | |
|
Re: Sort an array which contains date formatted elements
by cdarke (Prior) on Jul 17, 2007 at 13:26 UTC | |
|
Re: Sort an array which contains date formatted elements
by snopal (Pilgrim) on Jul 17, 2007 at 14:45 UTC | |
by msk_0984 (Friar) on Jul 20, 2007 at 06:40 UTC | |
|
Re: Sort an array which contains date formatted elements
by salva (Canon) on Jul 17, 2007 at 16:35 UTC | |
|
Re: Sort an array which contains date formatted elements
by dsheroh (Monsignor) on Jul 17, 2007 at 15:39 UTC | |
|
Re: Sort an array which contains date formatted elements
by injunjoel (Priest) on Jul 17, 2007 at 18:29 UTC | |
by johngg (Canon) on Jul 17, 2007 at 22:13 UTC |