How may I structure a command to print only 2.60 from the following string (the output from another command, lscpu):
model name : Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz
How may I structure a command to print only 2.60 from the following string (the output from another command, lscpu):
model name : Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz
Assuming you mean to print the number between @<whitespaces> and GHz at the end of lines that start with model name, that would be:
sed -nE 's/^model name.*@[[:space:]]*([.[:digit:]]+)GHz$/\1/p' /proc/cpuinfo
Or with GNU grep:
grep -Po '^model name.*@\s*\K[.\d]+(?=GHz$)' /proc/cpuinfo
With lscpu, see also:
lscpu -J | jq -r '
.lscpu[] |
select(.field == "Model name:").data |
match("@\\s*([\\d.]+)GHz$").captures[0].string |
tonumber'
Or:
lscpu -J | jq -r '.lscpu[]|select(.field == "CPU max MHz:").data|tonumber'
For the max frequency in MHz instead of the base frequency from the model name.
lspcu to those grep/sed commands, but on my system at least, there's no line starting with model name (Model name instead), so you'd need to adapt it. See also the -i option of grep (or the i/I modifiers for s command of GNU sed) for case-insensitive matching
Using sed (GNU sed for its I flag for case insensitive matching).
$ lscpu | sed -En 's/^model name.* ([0-9.]+).*/\1/Ip'
2.60
You can try something like this with awk:
awk '{print int($NF*100)/100.0}' /proc/cpuinfo
this line can be rewrited but I am not sure how different awk implementations will handle it:
awk '{print $NF*1.0}' /proc/cpuinfo
This will print 2.6. If you want to have it with zero you can use:
awk '{printf "%4.2f",int($NF*100)/100.0}' /proc/cpuinfo
If you want to use lscpu you can it on this way:
lscpu|awk '/odel name/ {print int($NF*100)/100.0}'
<command>| awk '{gsub(/[a-zA-Z]/,"",$NF);print $NF}'
Tested with echoing the following command output
echo "model name : Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz"| awk '{print $NF}'|sed "s/[a-zA-Z]//g"
output
2.60
LANG=C lscpu | sed -rn "s/.*Model name.* @ (.*)GHz/\1/p"
The LANG=C is needed, if your language setting isn't set to English.
The -n for sed suppresses output by default. The -r is for simpliefied typing of capturing groups (without backslash).
The "s/A/B/p" command substitutes A with B and the p performs a print, if condition A is met (since we suppressed output by default, that's neccessary).
A is the regex, and \1 the first (and only) capture group.