6

I'm aware that I can somehow sort this output numerically (so cpu1/ follows cpu0/) ... I could probably get something to work eventually by splitting up the string various ways with awk, but is there an "easy" way that would be somewhat reusable in the future in other scripts?

> 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
8

The "version sort" seems to work fine with this.

for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq; 
   do echo -n "$i: "; cat $i; done | sort -V
2
  • It worked on my Debian box – rahmu Oct 27 '11 at 9:33
  • Just an addendum...sort on my Ubuntu 8.04 server doesn't have that option...it's there on my RHEL6 boxes though. – twblamer Oct 27 '11 at 9:48
2

This should work with most sort implementations:

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

Trying to set an example (cannot find cpufreq on the systems I have access to ...):

input file

/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 

output:

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
3
  • That doesn't work with my example on either RHEL 6.1 or Ubuntu 8.04. – twblamer Oct 27 '11 at 20:45
  • Hi @twblamer, I've just added an example in my post. Could you please post the output that you get? – Dimitre Radoulov Oct 27 '11 at 20:51
  • 1
    False alarm...I was using different input data than what I initially posted. It's working now...sorry. – twblamer Oct 27 '11 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.