我如何对这些目录名称进行数字排序?

我如何对这些目录名称进行数字排序?

我知道我可以以某种方式对这个输出进行数字排序(因此 cpu1/ 跟随 cpu0/)...我可能最终可以通过使用 awk 将字符串以各种方式拆分来得到一些工作,但是有没有一种“简单”的方法将来在其他脚本中可以重用吗?

> for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq; do echo -n "$i: "; cat $i; done
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: 2395000
/sys/devices/system/cpu/cpu10/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu11/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu12/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu13/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu14/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu15/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu16/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu17/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu18/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu19/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu20/cpufreq/scaling_cur_freq: 1064000

答案1

“版本排序”似乎可以很好地解决这个问题。

for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq; 
   do echo -n "$i: "; cat $i; done | sort -V

答案2

这应该适用于大多数种类实施:

 ... | sort -t/ -k6.4n

尝试设置一个示例(在我有权访问的系统上找不到 cpufreq ...):

输入文件

/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: 2395000
/sys/devices/system/cpu/cpu10/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu11/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu12/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu13/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu14/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu15/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu16/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu17/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu18/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu19/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu20/cpufreq/scaling_cur_freq: 1064000 

输出:

zsh-4.3.12[t]% sort -t/ -k6.4n infile
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: 2395000
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu10/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu11/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu12/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu13/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu14/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu15/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu16/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu17/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu18/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu19/cpufreq/scaling_cur_freq: 1064000
/sys/devices/system/cpu/cpu20/cpufreq/scaling_cur_freq: 1064000

相关内容