按后缀排序列表

Sort list by suffix

我想了解如何按特定字符串结尾对纯文本文件进行排序,在本例中为后缀 'ly':

aback
abaft
abandonedly
abashedly
abeam
abed
abhorrently
... 

所有以“ly”结尾的单词都从该列表中提取和/或拆分,它应该类似于以下内容:

aback
abaft
abeam
abed
...
abandonedly
abashedly
abhorrently

注:

我愿意使用编程语言、命令行或服务。事实上,根据我之前的在线研究,我无法确定哪种方法最有效或最简单地完成这种排序。

Python中的解决方案:

with open("input.txt", "r") as f_in:
    lines = sorted(map(str.strip, f_in), key=lambda k: (k.endswith("ly"), k))

with open("output.txt", "w") as f_out:
    print("\n".join(lines), file=f_out)

来自input.txt

aback
abaft
abandonedly
abashedly
abeam
abed
abhorrently

创建 output.txt:

aback
abaft
abeam
abed
abandonedly
abashedly
abhorrently