Re: Fwd: suggestion to handle stock Fedora 8 logrotate changed extension on archived log files

Mike Tremaine <[email protected]>
Newsgroups gmane.comp.log.logwatch.devel
Message-ID <[email protected]>
There is one last issue which is what is the logic of the array sort?

Before it was oldest to newest or decending from higest # to lowest.  
But if it a dated file you do not get that same order. Consider this  
directory and this snip of  code for testing.

Rigel:~/Projects/logwatch_cvs mgt$ ls -al test
total 0
drwxr-xr-x   10 mgt  mgt  340 May  1 10:11 ./
drwxrwxr-x   13 mgt  mgt  442 May  1 14:33 ../
-rw-r--r--    1 mgt  mgt    0 May  1 10:04 message
-rw-r--r--    1 mgt  mgt    0 May  1 10:03 message-20081129
-rw-r--r--    1 mgt  mgt    0 May  1 10:03 message-20081229
-rw-r--r--    1 mgt  mgt    0 May  1 10:03 message-20081230
-rw-r--r--    1 mgt  mgt    0 May  1 10:03 message-20081231
-rw-r--r--    1 mgt  mgt    0 May  1 10:03 message.1
-rw-r--r--    1 mgt  mgt    0 May  1 10:03 message.2
-rw-r--r--    1 mgt  mgt    0 May  1 10:11 secure

--Code--
#!/usr/bin/perl

@TempLogFileListold = reverse(glob("./test/*"));

print "Old\n";
for my $file (@TempLogFileListold) {
print "$file\n";
}

@TempLogFileList = sort{
    ($b =~ /(\d+)$/) <=> ($a =~ /(\d+)$/) || uc($a) cmp uc($b)
}(glob("./test/*"));

print "New\n";

for $file (@TempLogFileList) {
print "$file\n";
}

exit;
--End Code--

You get these results

Rigel:~/Projects/logwatch_cvs mgt$ perl test.pl
Old
./test/secure
./test/message.2
./test/message.1
./test/message-20081231
./test/message-20081230
./test/message-20081229
./test/message-20081129
./test/message
New
./test/message-20081129
./test/message-20081229
./test/message-20081230
./test/message-20081231
./test/message.1
./test/message.2
./test/message
./test/secure

Notice in the new sort the .1 .2 is newest to oldest but the dated is  
oldest to newest. Changing the $a $b order around is simple enough  
but what is it we really want? Or does it really matter since most  
logfiles will be put through the date code?

-Mike
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.