linux - Bash - 根据多个字段对文件名进行排序

标签 linux bash unix

我有一个文件集合,例如

fa_1_se-1.xml  
fa_1_se-2.xml
fa_1_se-3.xml
fa_1_se-10.xml   
fa_2_se-1.xml  
fa_2_se-2.xml
fa_2_se-12.xml 

其中下划线后的数字表示person_id,连字符后的数字表示record_id。

我试图先按 person_id 对它们进行排序,然后按 record_id 对它们进行排序,但没有成功。

ls |排序 -t- -k2 给出

f_a_1_se-1.xml
f_a_2_se-1.xml
f_a_1_se-10.xml
f_a_2_se-12.xml
f_a_1_se-2.xml
f_a_2_se-2.xml
f_a_1_se-3.xml

如何让它按 person_id 排序,然后按 record_id 排序?

谢谢!

最佳答案

你可以使用--stable选项:

sort --stable -t_ -nk2 file
fa_1_se-1.xml
fa_1_se-2.xml
fa_1_se-3.xml
fa_1_se-10.xml
fa_2_se-1.xml
fa_2_se-2.xml
fa_2_se-12.xml

编辑:

你也可以试试:

printf "%s\n" fa* | tr '-' '_' | sort -t_ -nk2 -nk4 | sed 's/_\([^_]*\)$/-\1/'

关于linux - Bash - 根据多个字段对文件名进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29418089/

相关文章:

c++ - 请解释此链接错误 : referenced in section . rodata

c - 如何更改内核 I/O 缓冲区大小

node.js - 从 Google Cloud Platform App Engine 启动脚本运行永久脚本

linux - 通过lftp从远程服务器复制文件

c - "iovcnt"中 "writev"参数的允许最大值?

linux - 命令 "r--"命令的 UNIX Shell 脚本问题未找到

linux - 动态修改数据流

linux - 在 bash 脚本中运行 Matlab; matlab : not found

linux - Bash 命令在更改 Path 变量后不起作用

linux - 比较前 x 个字符的文件