Sort from start index and end index in line

Hi All,

I have a file (FileNames.txt) which contains the following data in it.
$ cat FileNames.txt
MYFILE17XXX208Sep191307.csv
MYFILE19XXX208Sep192124.csv
MYFILE20XXX208Sep192418.csv
MYFILE22XXX208Sep193234.csv
MYFILE21XXX208Sep193018.csv
MYFILE24XXX208Sep194053.csv
MYFILE23XXX208Sep193837.csv
$

I want to sort these files names using the number avaialable after the "08Sep" in each file name. The starting index of this number in this filename string will be 17 and end index will 22.

Please help me on how to sort these file names using this number.

Cheers ,
Krish.

sort -k1.11,1.17 -n  -o myfile.txt myfile.txt

Are you sure the starting index is 17 (not 18) and the ending index is 22 (not 23) since you say the number after the "08Sep" string...

sort -k1.18,1.23 infile

Hi jim mcnamara, shamrock,

Thanks for your great support .. It worked well.. :slight_smile:

Shamrock,

Yes, the index should be 18, 23. The thought the string's staring index is 0, that is the reason I have put the index as 17,22.
Do we need to consider the start index as 1 for sort ?? Just curious. :confused:

Thanks again!!!!!!!

Cheers,
Krish.

open FH,"<b.txt";
my @arr=<FH>;
print  map {$_->[0]} 
          sort {$a->[1] <=> $b->[1]} 
            map {/.*([0-9]{6})\..*/;[$_,$1]} @arr;

Can you please explain why you use 1.18 and 1.23 above?
Can't we use simple 18 and 23 like below

sort -k18,23 infile