开发者

How can I sort a list of strings by numbers in them?

开发者 https://www.devze.com 2023-03-11 03:13 出处:网络
I have a list of filenames which are like so: fw_d.log.1.gz through fw_d.log.300.gz When I use this code block below, it almost sorts it the way I want, but not quite:

I have a list of filenames which are like so:

fw_d.log.1.gz  
through  
fw_d.log.300.gz  

When I use this code block below, it almost sorts it the way I want, but not quite:

#!/usr/bin/perl -w
my $basedir = "/var/log";
my @verdir = qw(fw_d);
my $fulldir;
my $configs;
my $combidir;

foreach $combidir (@verdir) {
    $fulldir = "$basedir/$combidir";
    opendir (DIR, $fulldir);
    my @files = grep { $_ ne '.' && $_ ne '..' && $_ ne 'CVS' readdir DIR;
    closedir (DIR);
    @files1 = sort {$a cmp $b}(@files);
    foreach my $configs (@files1) {
        print "Checking $configs\n";
        system("less $basedir/$combidir/$configs | grep \'.* Group = , Username = .* autheauthenticated.\' >> output.log" );
    }
}

Here is a snippet output:

Checking fw_d.log  
Checking fw_d.log.1.gz  
Checking fw_d.log.10.gz  
Checking fw_d.log.100.gz  
Checking fw_d.log.101.gz  
Checking fw_d.log.102.gz  

As you can see, it almost sorts it how开发者_Go百科 I was hoping... Does anyone have any suggestions, on either reading, or a code snippet I can use?


You could use Schartzian-transform :

my @sorted = map  { $_->[0] }
             sort { $a->[1] <=> $b->[1] }
             map  { [$_, $_=~/(\d+)/] }
                 @files;
print Dumper \@sorted;

Added benchmark for comparison between Schwartzian-Transform and subroutine

use Benchmark qw(:all);

# build list of files
my @files = map {'fw_d.log.'.int(rand()*1000).'.log' } 0 ..300;

my $count = -3;
my $r = cmpthese($count, {
        'subname' => sub {
              sub expand {
                   my $file=shift; 
                   $file=~s{(\d+)}{sprintf "%04d", $1}eg;
                   return $file;
              }
              my @sorted = sort { expand($a) cmp expand($b) } @files;
        },
        'schwartzian' => sub {
              my @sorted = map  { $_->[0] }
                           sort { $a->[1] <=> $b->[1] }
                           map  { [$_, $_=~/(\d+)/] }
                 @files;
         }
});

Result:

              Rate     subname schwartzian
subname     21.2/s          --        -92%
schwartzian  279/s       1215%          --

Schwartzian-transform is about 13 times more efficient for sorting 300 files.


the problem is that the code does what you tell it to do: sort the file names in alphabetical order.

You should replace sort { $a cmp $b } by sort { expand($a) cmp expand($b) }

with expand:

sub expand 
   { my $file=shift; 
     $file=~s{(\d+)}{sprintf "%04d", $1}eg; # expand all numbers to 4 digits
     return $file;
   }


What you can try is using a custom sort function:

sub sort_by_number {
    $a =~ /(\d+)/;
    $numa = $1;
    $b =~ /(\d+)/;
    $numb = $1;

    return $numa <=> $numb;
}

and then sort like this:

@files1 = sort sort_by_number @files;

This will sort the strings in @files by the value of the first number in each string.


Older question, but there's an answer as yet unmentioned.

Sort::Naturally does this for you:

Sort lexically, but sort numeral parts numerically

#!/usr/bin/env perl
use strict;
use warnings;

use Sort::Naturally;

print nsort <DATA>;

__DATA__
fw_d.log  
fw_d.log.101.gz  
fw_d.log.1.gz  
fw_d.log.10.gz  
fw_d.log.100.gz 
fw_d.log.2.gz  
fw_d.log.102.gz  
fw_d.log.12.gz

This orders as:

fw_d.log  
fw_d.log.1.gz  
fw_d.log.2.gz  
fw_d.log.10.gz  
fw_d.log.12.gz
fw_d.log.100.gz 
fw_d.log.101.gz  
fw_d.log.102.gz  
0

精彩评论

暂无评论...
验证码 换一张
取 消