1

I currently have this code:

cat /proc/cpuinfo | grep "MHz" | cut -d ':' -f2 | sed 's/.*/Core NUM&Mhz/'

Which outputs the following (for 8 cores):

Core NUM 1941.054Mhz
Core NUM 1949.820Mhz
Core NUM 2022.734Mhz
Core NUM 1877.171Mhz
Core NUM 1938.265Mhz
Core NUM 1945.703Mhz
Core NUM 1845.562Mhz
Core NUM 1781.546Mhz

What I want to do is replace "Core NUM" with actual numbers (e.g. Core 0, Core 1, Core 2, Core 3...) I would prefer a solution that can be done on one line with the rest of this command, but since I am in the end working from a bash script, I don't mind a multi-line/scripted solution either. The biggest problem I guess is that grep is one command outputting many lines, and I don't know if there's a way to target these lines one by one in something like a for or while loop.

So, how can I do this?

8
  • You can probably replace the grep, cut and sed with awk and get awk to print the line numbers as well - someone will be along soon with an awk solution, I'm sure. Mar 2, 2016 at 12:45
  • @EightBitTony you called it!
    – Cestarian
    Mar 2, 2016 at 12:50
  • yeh, it's not a race, but @cuonglm beat me by about 12 seconds. I was too busy mis-typing /proc/cpuinfo as /proc/procinfo and wondering why it wasn't there. Mar 2, 2016 at 12:51
  • You do know you can use cpupower for that, don't you ? e.g. cpupower -c all frequency-info -fm or, to format it per your question cpupower -c all frequency-info -fm | paste -d' ' - - | cut -c11- (assuming en_US locale) Mar 2, 2016 at 13:24
  • @don_crissti I had no idea about cpupower, and it is irrelevant. I wanted to do this with a stock linux installation (without extra software, which cpupower as far as i'm concerned is) hell, cpupower doesn't even seem to exist in my distro's package repositories.
    – Cestarian
    Mar 2, 2016 at 13:43

3 Answers 3

5

Let awk do it all for you:

</proc/cpuinfo awk -F : '/MHz/{printf "Core %d%sMhz\n", n++, $2}'
1
  • Great, out of the two answers provided, I think this one is cooler because it's all one command. I'll mark it as an answer if you can show me how to append "MHz" at the end like the other one did :)
    – Cestarian
    Mar 2, 2016 at 12:52
3

cat /proc/cpuinfo | awk '/MHz/ {print "Core", i++, $NF "Mhz"}' gives,

Core 0 800.000Mhz
Core 1 800.000Mhz
3

I did a slightly different awk version, but once you are familiar with awk, it will stand out as the natural solution for this. I used the following:

cat /proc/cpuinfo | awk -F : '/^processor/ { x = $2 } /^cpu MHz/ { print "CPU" x ":" $2 " MHz" }'

which uses a slightly different approach of saving off the value of the 'processor' field and then recalling it when the cpu MHz field is encountered. You can do slightly more general things this way, provided you know which of the fields you are interested in occurs last. In this case, we know the 'cpu MHz' comes after the 'processor' field, so we know that when 'cpu MHz' is encountered, the value stored in the variable x will be correct.

Output looks like:

CPU 0: 887.597 MHz
CPU 1: 818.359 MHz
CPU 2: 887.597 MHz
CPU 3: 803.222 MHz

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .