S
sunckell
Greetings,
I am having a little difficulty sorting unix non-compressed logs
files that are in an array.
The log files ( as usual ) end in a number ranging from nothing (such
as mail.debug) to 30 (such as mail.debug.30).
My sort is mostly working ( thanks to the help of perldoc -f sort )
but it spits a warning because my first log file doesn't have a
numberical ending.
Warning message: Use of uninitialized value in numeric comparison (<=>)
at
$script line number...
Can anyone offer a better way to sort the array of log files?
-------------------------------------------------------------------------------------------
use strict;
use warnings;
use sort 'stable';
use diagnostics;
use POSIX qw(fstat);
use Sys::Hostname;
$|++;
use vars qw(
$log_dir $hostname $maillog_dir
@log_files @mail_logs_unsorted @mail_logs_sorted
);
$0 = $1 if $0 =~ m:/([^/]+)$:;
$hostname = hostname();
$maillog_dir = '/var/log';
# get list of log_files in $log_dir
$log_dir = POSIX:pendir( $maillog_dir );
@log_files = POSIX::readdir( $log_dir );
POSIX::closedir( $log_dir );
#cut the list down to only mail logs
foreach my $log ( @log_files ){
if($log =~ /^mail\.debug/){
push( @mail_logs_unsorted, "$log" );
}
}
undef @log_files; #don't need that anymore
@mail_logs_sorted = sort { substr($a, 11) <=> substr($b, 11) }
@mail_logs_unsorted;
foreach my $log ( @mail_logs_sorted ) {
print "$log\n";
}
Thanks for the help,
SunCkell
I am having a little difficulty sorting unix non-compressed logs
files that are in an array.
The log files ( as usual ) end in a number ranging from nothing (such
as mail.debug) to 30 (such as mail.debug.30).
My sort is mostly working ( thanks to the help of perldoc -f sort )
but it spits a warning because my first log file doesn't have a
numberical ending.
Warning message: Use of uninitialized value in numeric comparison (<=>)
at
$script line number...
Can anyone offer a better way to sort the array of log files?
-------------------------------------------------------------------------------------------
use strict;
use warnings;
use sort 'stable';
use diagnostics;
use POSIX qw(fstat);
use Sys::Hostname;
$|++;
use vars qw(
$log_dir $hostname $maillog_dir
@log_files @mail_logs_unsorted @mail_logs_sorted
);
$0 = $1 if $0 =~ m:/([^/]+)$:;
$hostname = hostname();
$maillog_dir = '/var/log';
# get list of log_files in $log_dir
$log_dir = POSIX:pendir( $maillog_dir );
@log_files = POSIX::readdir( $log_dir );
POSIX::closedir( $log_dir );
#cut the list down to only mail logs
foreach my $log ( @log_files ){
if($log =~ /^mail\.debug/){
push( @mail_logs_unsorted, "$log" );
}
}
undef @log_files; #don't need that anymore
@mail_logs_sorted = sort { substr($a, 11) <=> substr($b, 11) }
@mail_logs_unsorted;
foreach my $log ( @mail_logs_sorted ) {
print "$log\n";
}
Thanks for the help,
SunCkell