终端:sort命令;如何正确排序?

发布于 2025-02-01 03:15:58 字数 352 浏览 2 评论 0原文

我写了一个shell脚本,该脚本从文件夹及其所有子文件夹中获取所有文件名,并在排序后将它们复制到剪贴板(删除所有路径;我只需要一个随机命名的文件的简单文件列表之内)。

我不知道如何使排序命令正确排序。意思是电子表格会整理内容的方式。或您的Mac查找器分类的方式。

下划线>数字>信件(无论情况如何)

有人知道该怎么做吗? sort -n仅适用于以数字开头的文件,sort -f是接近的,但以怪异的方式分开了较低的情况和首都,而以数字开头的任何东西都是在这个地方。 sort -v是最接近的,但是任何始于下划线的一切都落在底部而不是顶部……我将失去理智。

I have written a shell script that gets all the file names from a folder, and all its sub-folders, and copies them to the clipboard after sorting (removing all paths; I just need a simple file list of the thousands of randomly named files within).

What I can’t figure out is how to get the SORT command to sort properly. Meaning, the way a spreadsheet would sort things. Or the way your Mac finder sorts things.

Underscores > numbers > letters (regardless of case)

Anyone know how to do this? Sort -n only works for files starting with numbers, sort -f was close but separated the lower case and capitals in a weird way, and anything starting with a number was all over the place. Sort -V was the closest, but anything started with an underscore went to the bottom instead of the top… I’m about to lose my mind. ????

I’ve been trying to figure this out for a week, and no combination of anything I have tried gets the sort command to actually, ya know, sort properly.

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

絕版丫頭 2025-02-08 03:15:58

如果我正确理解问题,则需要如对人类进行分类:自然排序顺序 ,and macos- Finder在包含数字和字符时如何进行排序文件夹?

使用Linux sort(1)您需要-v- 版本 - sort)选项“自然”排序。您还需要-f- ignore-case)选项来忽略字母的情况。因此,假设文件名是在一个名为files.txt的文件中存储单行单行的,则可以以您想要的方式制作(主要是)列表:

sort -Vf files.txt

但是,>排序-VF在我的系统上的数字和字母后提出了指数。我已经尝试使用不同的语言(请参阅如何在当前终端的会话中设置语言环境?),但没有成功。我看不到使用sort选项更改此操作的方法(但我可能缺少某些内容)。

字符似乎在数字和字母之前与sort -v进行排序。解决问题的可能攻击方法是与其中一个交换,分类,然后再次交换。例如:

tr '_~' '~_' <files.txt | LC_ALL=C sort -Vf |  tr '_~' '~_'

似乎在我的系统上做您想要的。我已经明确将sort命令的语言环境设置为lc_all = c ...,因此在其他系统上应该行为相同。 (请参阅为什么 sort 在每台机器上都不相同?。)。

If I understand the problem correctly, you want the "natural sort order" as described in Natural sort order - Wikipedia, Sorting for Humans : Natural Sort Order, and macos - How does finder sort folders when they contain digits and characters?.

Using Linux sort(1) you need the -V (--version-sort) option for "natural" sort. You also need the -f (--ignore-case) option to disregard the case of letters. So, assuming that the file names are stored one-per-line in a file called files.txt you can produce a list (mostly) sorted in the way that you want with:

sort -Vf files.txt

However, sort -Vf sorts underscores after digits and letters on my system. I've tried using different locales (see How to set locale in the current terminal's session?), but with no success. I can't see a way to change this with sort options (but I may be missing something).

The characters . and ~ seem to consistently sort before numbers and letters with sort -V. A possible hack to work around the problem is to swap underscore with one of them, sort, and then swap again. For example:

tr '_~' '~_' <files.txt | LC_ALL=C sort -Vf |  tr '_~' '~_'

seems to do what you want on my system. I've explicitly set the locale for the sort command with LC_ALL=C ... so it should behave the same on other systems. (See Why doesn't sort sort the same on every machine?.)

青芜 2025-02-08 03:15:58

It appears you want to sort in dictionary order and fold case, so it would be sort -df.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文