Home > Back-end >  How to sort filenames witch contains numbers
How to sort filenames witch contains numbers

Time:06-09

I have a list of filenames, cat mylist.txt:

myfile.txt
myfile.version2.txt
myfile.version.tab.txt

If I use in Python sorted() function, I have this result (that what I except):

myfile.txt
myfile.version.tab.txt
myfile.version2.txt

I would like to have the same in Bash, result of cat mylist.txt | sort -V:

myfile.txt
myfile.version2.txt
myfile.version.tab.txt

Result of cat mylist.txt | sort:

myfile.txt
myfile.version2.txt
myfile.version.tab.txt

It's not what I expect. How can I sort properly with the most generic way? For example, imagine that one day I will have this in my list:

myfile.version.tab2.txt
myfile.version.tab.txt

CodePudding user response:

Locale issue? Try:

$ LC_ALL=C sort mylist.txt
myfile.txt
myfile.version.tab.txt
myfile.version.tab2.txt
myfile.version2.txt

CodePudding user response:

Are you sure about your issue? I have done a test and this is my result:

cat mylist.txt | sort
version.txt
version2.txt

cat mylist.txt | sort -V
version.txt
version2.txt

echo $SHELL
/bin/bash

I've done my test on a Ubuntu app:

uname -a
Linux name 4.4.0-19041-Microsoft #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 x86_64 x86_64 x86_64 GNU/Linux
  • Related