简体   繁体   English

如何按编号对linux中的文件进行排序?

[英]How to sort files in linux by number?

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? 我想要它的确切排序,但是,在这个例子中,列表为500,4000,7000。我的命名约定应该如何实现呢? 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. 我真正想要的是它按等级排序(这里从零开始),然后按递增的数字对它进行排序.. 500,5000,7000。

在此输入图像描述

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. 也许我的问题是转向开发一个自定义方法来读取我提供的排序约束下的文件。

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: return新的已排序list而不是修改原始list

>>> 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM