我正在排序一个find命令的结果,该命令查找当前目录中的所有文件:
find . -maxdepth 1 -type f -iname "*.flac" | sort我期待的是这样一份清单:
./Track 1.flac
./Track 2.flac
./Track 3.flac
...
./Track 9.flac
./Track 10.flac
./Track 11.flac我得到的是这样一张清单:
./Track 10.flac
./Track 11.flac
./Track 1.flac
./Track 2.flac
./Track 3.flac
...
./Track 9.flac是否有sort选项,将它们按字母数字升序排列,以便正确地计算数字?
发布于 2012-06-19 19:51:50
尝试将-n和-k2命令行选项传递给sort。也就是说,
find . -maxdepth 1 -type f -iname "*.flac" | sort -n -k2当我将未排序的文件名放入文件“data.txt”并运行以下命令时:
sort -k2 -n data.txt我得到这个作为输出:
./Track 1.flac
./Track 2.flac
./Track 3.flac
./Track 9.flac
./Track 10.flac
./Track 11.flac备选方案说明:
-n (numeric sort) compare according to string numerical value
-k2 means sort on the 2nd field (and to the end of the line),
you could just restrict it to the second field with -k2,2你没有问这个问题,我也没有在上面用过,但总有一天它会派上用场的。
-r reverse sort order 手册页 for sort
请参阅我关于按不同字段排序的相关文章,按第三列排序,保留第一列和第二列完整(在linux中)解释了更多关于排序命令的内容。
发布于 2012-06-20 23:42:16
在zsh中,使用n 球限定符:
print -l *.flag(n)https://unix.stackexchange.com/questions/41149
复制相似问题