I am trying to get a simplified output of the cpu frequency to 3 digits, such that if the frequency is less than 1000 it will output xxx Mhz
and if it's above 1000 it will outpute x.xx Ghz
. I can get only the frequency with lscpu | sed -n 's/CPU MHz:[ \t]*//p'
, and the first 4 digits with lscpu | sed -n 's/CPU MHz:[ \t]*//p' | cut -c1-4
, however I'm not sure how to parse this to achieve the desired result.
Try:
How it works
/CPU MHz/{...}
This selects the line we want and executes the commands in curly braces only for that line.
if($NF+0>1000)printf "%.3f GHz\n",$NF/1000; else printf "%.3f MHz\n",$NF
If the final field,
$NF
, is greater than 1000, then we divide it by 1000 and print it as GHz. Otherwise, we print it as MHz. The format%.3f
determines how many significant digits are printed. Adjust this to suit your tastes.Multi-line version
For those who prefer their commands spread over multiple lines: