Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 8 down vote accepted

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
share|improve this answer
    
It worked on my Debian box –  rahmu Oct 27 '11 at 9:33
    
Interesting...works for me. Thanks. –  twblamer Oct 27 '11 at 9:43
    
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

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
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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