Search code examples
bashawksed

Linux CPU string by Bash


I try to build a string that identifies my CPU.

cat /proc/cpuinfo  | grep "model name"

model name  : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name  : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name  : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name  : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name  : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name  : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name  : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
model name  : Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz

What I want is the string just once and the number of cores.

Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz - 8 Cores

How can I get this with bash/awk/sed the best way?

Otherways I would do it with perl but I think it should work with piping too.


Solution

  • You could let count the matching lines and append that to the result:

    awk -F ': ' \
        '/model name/ {cpu=$2; count++} END {print cpu " - " count " Cores"}' \
    /proc/cpuinfo
    

    Outputs the following on my (ancient) machine:

    Intel(R) Xeon(R) CPU           X3430  @ 2.40GHz - 4 Cores
    

    AWK can read directly from the file, so I've removed the useless cat call.