use*_*548 27 bash shell-script text-processing sort
这是我要排序的数据。但是sort将数字处理为字符串,数据没有按我预期的那样排序。
/home/files/profile1
/home/files/profile10
/home/files/profile11
/home/files/profile12
/home/files/profile14
/home/files/profile15
/home/files/profile16
/home/files/profile2
/home /files/profile3
/home/files/profile4
/home/files/profile5
/home/files/profile6
/home/files/profile7
/home/files/profile8
/home/files/profile9
我想把这个排序,
/home/files/profile1
/home/files/profile2
/home/files/profile3
/home/files/profile4
/home/files/profile5
/home/files/profile6
/home/files/profile7
/home/files/profile8
/home /files/profile9
/home/files/profile10
/home/files/profile11
/home/files/profile12
/home/files/profile14
/home/files/profile15
/home/files/profile16
bash脚本有什么好方法吗?我不能在这里使用 ruby 或 python 脚本。
Tho*_*hor 46
这与这个问题非常相似。问题是您有一个用于排序的字母数字字段,并且-n没有明智地对待它,但是 version sort ( -V) 却如此。因此使用:
sort -V
Run Code Online (Sandbox Code Playgroud)
请注意,目前 GNU、FreeBSD 和 OpenBSD 排序实现支持此功能。
max*_*zig 21
您可以使用临时标记字符来分隔数字:
$ sed 's/\([0-9]\)/;\1/' log | sort -n -t\; -k2,2 | tr -d ';'
Run Code Online (Sandbox Code Playgroud)
这里,哨兵字符是';' - 它不能是您要排序的任何文件名的一部分 - 但您可以交换“;” 用你喜欢的任何角色。您必须相应地更改sed,sort和trpart 然后。
管道的工作原理如下:该sed命令在任何数字之前插入哨兵,该sort命令将哨兵解释为字段分隔符,将第二个字段作为数字排序键进行排序,该tr命令再次删除哨兵。
并log表示输入文件 - 您也可以将输入通过管道传输到sed.
如果所有文件名在最后一个数字部分之前都有相同的前缀,请在排序时忽略它:
sort -k 1.20n
Run Code Online (Sandbox Code Playgroud)
(20 是第一个数字的位置。它是 1 加上 的长度/home/files/profile。)
如果您有几个不同的非数字部分,请插入一个标记。