Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort files in linux by number?

Tags:

python

linux

My program reads in the number parsed from the file name. I want it to be ordered exactly how it is, but, in this example, list as 500, 4000, 7000. How should my naming convention look to achieve this? That is, when I have incrementing numbers, it lists it from smallest to highest.

What I really want is for it to sort by rank, (which here starts at zero), then sorts it by the incrementing numbers.. 500, 5000, 7000.

enter image description here

DESIRED OUTPUT

LOG-rank-0-die-10-delay-500.txt
LOG-rank-0-die-10-delay-4000.txt
LOG-rank-0-die-10-delay-7000.txt
LOG-rank-1-die-10-delay-500.txt
LOG-rank-1-die-10-delay-4000.txt
LOG-rank-1-die-10-delay-7000.txt
LOG-rank-2-die-10-delay-500.txt
LOG-rank-2-die-10-delay-4000.txt
LOG-rank-2-die-10-delay-7000.txt

Relevant Code

for filenamelogs in sorted(os.listdir(log_directory)):
            for each_line in filenamelogs:
                   #various file parsing activity

I'm appending the data file-by-file to various arrays. Unfortunately, this is terrible to me if I can't sort the file reads in the order requested. Maybe my question is veered toward developing a custom method to read in files under the sorting constraints I provide.

like image 442
Ryan Avatar asked Feb 03 '26 17:02

Ryan


1 Answers

From a comment on a blog linked in a blog:

>>> import re
>>> def sort_nicely(l):
...     """
...     Sort the given list in the way that humans expect. Modifies the original list.
...     """
...     convert = lambda text: int(text) if text.isdigit() else text
...     alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
...     l.sort(key=alphanum_key)
...
>>> mylist = ['LOG-rank-0-die-10-delay-500.txt',
... 'LOG-rank-0-die-10-delay-4000.txt',
... 'LOG-rank-0-die-10-delay-7000.txt',
... 'LOG-rank-1-die-10-delay-500.txt',
... 'LOG-rank-1-die-10-delay-4000.txt',
... 'LOG-rank-1-die-10-delay-7000.txt',
... 'LOG-rank-2-die-10-delay-500.txt',
... 'LOG-rank-2-die-10-delay-4000.txt',
... 'LOG-rank-2-die-10-delay-7000.txt']
>>> sort_nicely(mylist)
>>> print(*mylist, sep='\n')
LOG-rank-0-die-10-delay-500.txt
LOG-rank-0-die-10-delay-4000.txt
LOG-rank-0-die-10-delay-7000.txt
LOG-rank-1-die-10-delay-500.txt
LOG-rank-1-die-10-delay-4000.txt
LOG-rank-1-die-10-delay-7000.txt
LOG-rank-2-die-10-delay-500.txt
LOG-rank-2-die-10-delay-4000.txt
LOG-rank-2-die-10-delay-7000.txt

To return a new, sorted list instead of modifying the original one in place:

>>> def sort_nicely(l):
...     """
...     Sort the given list in the way that humans expect. Returns a new list.
...     """
...     convert = lambda text: int(text) if text.isdigit() else text
...     alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
...     return sorted(l, key=alphanum_key)
...
>>> newlist = sort_nicely(mylist)
>>> print(*newlist, sep='\n')
like image 56
TigerhawkT3 Avatar answered Feb 05 '26 07:02

TigerhawkT3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!