이러한 디렉터리 이름을 숫자로 정렬하려면 어떻게 해야 합니까?

이러한 디렉터리 이름을 숫자로 정렬하려면 어떻게 해야 합니까?

나는 이 출력을 어떻게든 수치적으로 정렬할 수 있다는 것을 알고 있습니다(그래서 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

관련 정보