I am making a conky config and I want to display the CPU model as one of the stats. For this, I need a command that outputs just the CPU name as a string, for example, "Intel Core i7 7700K". I think both /proc/cpuinfo and the output from lscpu have what I need (I would prefer lscpu because it would be slightly more efficient), but the model name is 4 spaces too far away from the manufacturer name. So I would need to cut off the model name: string and get rid of the extra spaces. I would also like to know how to get rid of the @ (clockspeed).
Asked
Active
Viewed 3.6k times
16
Haxalicious
- 503
3 Answers
30
$ lscpu | sed -nr '/Model name/ s/.*:\s*(.*) @ .*/\1/p'
Intel(R) Atom(TM) CPU Z3735F
Notes
-ndon't print anything until we ask for it-ruse ERE/Model name/find the line withModel names/old/new/replaceoldwithnew.*:\s*anything before a colon, a colon, and any amount of horizontal space(.*) @save any number of characters before' @'\1reference to saved patternpprint only the line we edited
To get rid of double spaces, you can do an extra s command:
$ lscpu | sed -nr '/Model name/ s/ / /g; s/.*:\s*(.*) @ .*/\1/p'
Intel(R) Atom(TM) CPU Z3735F
The doesn't work if there are more than two spaces anywhere, so to delete multiple spaces relentlessly, use a sed loop:
$ lscpu | sed -nr ':a;s/ / /;ta; /Model name/ s/.*: (.*) @ .*/\1/p'
Intel(R) Atom(TM) CPU Z3735F
This :a;s/ / /;ta keeps on processing the stream until there are no double spaces anywhere. Notice that it won't take an address, so we had to chew through the whole stream before selecting the line. This won't take a noticeable amount of time when we're only parsing the output of lscpu, but on a huge file or on multiple files, it could get pretty slow. We could pipe the output from the original command to another sed with the loop instead to avoid working on the whole stream:
$ lscpu | sed -nr '/Model name/ s/.*:\s*(.*) @ .*/\1/p' | sed ':a;s/ / /;ta'
Intel(R) Atom(TM) CPU Z3735F
Zanna
- 72,312
9
lscpu | grep 'Model name' | cut -f 2 -d ":" | awk '{$1=$1}1'
lscpuTo get the CPU informationgrep 'Model name'To extract the line containing CPU name.cut -f 2 -d ":"To remove part of the line before:. So it will remove the part "Model name:" from the output.awk '{$1=$1}1'To remove the space from the beginning of a line.
Sample Output
$ lscpu | grep 'Model name' | cut -f 2 -d ":" | awk '{$1=$1}1'
Intel(R) Pentium(R) CPU N3520 @ 2.16GHz
ran
- 395
- 2
- 8
5
Use this to get what you want:
lscpu | grep "Model name:" | sed -r 's/Model name:\s{1,}//g'
Result:
Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz
Or:
lscpu | grep "Model name" | sed -r 's/Model name:\s{1,}(.*) @ .*z\s*/\1/g'
Result:
Intel(R) Core(TM) i5-4210U CPU
George Udosen
- 37,534