Marius
Marius

Reputation: 1060

perl - ignore numbers in file name while sorting folder

I would like to sort the files in a folder based only on the non numeric part of the file name. The general pattern of a file name is as follows:

BLA_SomeText_2015-07-16_12-00-05_v2.6.6.6_OtherText.ext
BLA_SomeText_2015-07-16_12-00-05_v2.6.6.7_Other.ext

The expected order of the sorted files is the other way around as above:

BLA_SomeText_2015-07-16_12-00-05_v2.6.6.7_Other.ext
BLA_SomeText_2015-07-16_12-00-05_v2.6.6.6_OtherText.ext

Is there a variant of sort that I missed from the perl documentation or I need to use regex anyhow? Thank you for your help.

Upvotes: 1

Views: 109

Answers (2)

Borodin
Borodin

Reputation: 126742

A bigger data sample would be nice, but this program shows how to use a sort block to compare the strings with all digits removed

use utf8;
use strict;
use warnings;
use 5.010;

my @files = <DATA>;
chomp @files;

say for sort {
  my ($aa, $bb) = map tr/0-9//dr, $a, $b;
  $aa cmp $bb;
} @files;

__DATA__
BLA_SomeText_2015-07-16_12-00-05_v2.6.6.6_OtherText.ext
BLA_SomeText_2015-07-16_12-00-05_v2.6.6.7_Other.ext

output

BLA_SomeText_2015-07-16_12-00-05_v2.6.6.7_Other.ext
BLA_SomeText_2015-07-16_12-00-05_v2.6.6.6_OtherText.ext

Upvotes: 1

mpapec
mpapec

Reputation: 50657

It will remove all numbers and then string sort in ascending order,

my @arr = qw(
  BLA_SomeText_2015-07-16_12-00-05_v2.6.6.6_OtherText.ext
  BLA_SomeText_2015-07-16_12-00-05_v2.6.6.7_Other.ext
);

@arr = map $_->[0],
  sort { $a->[1] cmp $b->[1] }
  map [ $_, tr|0-9||dr ], @arr;

Upvotes: 3

Related Questions