You are not logged in.
I'm a sed noob, it's confusing.
All I'm trying to do is find my current CPU speed.
I've currently got
cat /proc/cpuinfo | grep "cpu MHz" | sed 's/\([a-zA-Z: \t]\)//g'
which outputs 800.000
So it removes all characters and whitespace, but what do I add to the expression to remove everything after the decimal point? This is driving me nuts and I'm sure the answer is so simple.
Thanks!
Last edited by ilovefridge (2009-02-23 22:58:25)
Offline
How about
grep "cpu MHz" /proc/cpuinfo | sed 's/\([a-zA-Z: \t]\)//g' | cut -d . -f 1
Not sed, but then again, I think cut is the right tool for the job here .
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
I'd use awk:
awk '/MHz/{ sub(/\..*/, "", $4); print $4 }' /proc/cpuinfo
Offline
I don't know awk... Should really start learning some
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
Awk +1, you can do nice things with it like
awk 'BEGIN { CONVFMT="%.1f" }/MHz/{ sub(/\..*/, "", $4); print $4/1024 "GHz" }' /proc/cpuinfo
To output in GHz with 1 decimal. You better make use of the advantage over sed.
And that won't affect speed.
Another one for fun, without regexes:
grep -F MHz /proc/cpuinfo | cut -b12-14
(where 12-14 would have to be 12-15 for >1000)
Last edited by Procyon (2009-02-24 00:46:13)
Offline
Awk +1, you can do nice things with it like
awk 'BEGIN { CONVFMT="%.1f" }/MHz/{ sub(/\..*/, "", $4); print $4/1024 "GHz" }' /proc/cpuinfo
To output in GHz with 1 decimal. You better make use of the advantage over sed.
And that won't affect speed.
Another one for fun, without regexes:
grep -F MHz /proc/cpuinfo | cut -b12-14
(where 12-14 would have to be 12-15 for >1000)
I know that these are just examples, but both of those produce wrong results on my system. The top says that my 3.2GHz processor is 3.1GHz, and the bottom one says 321MHz instead 3214MHz.
Offline
Another one for fun, without regexes:
grep -F MHz /proc/cpuinfo | cut -b12-14
(where 12-14 would have to be 12-15 for >1000)
Why the -F if I may ask? I don't see it make any difference in this particular case.
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
The top says that my 3.2GHz processor is 3.1GHz
because 1GHz = 1000 MHz, not 1024 MHz as the code assumes.
and the bottom one says 321MHz instead 3214MHz.
because the code assumes 3-digit clockspeed.
Offline
Procyon wrote:Another one for fun, without regexes:
grep -F MHz /proc/cpuinfo | cut -b12-14
Why the -F if I may ask? I don't see it make any difference in this particular case.
For the extra speed boost (of 1ms) it skips trying to interpret MHz as a regex.
skottish wrote:The top says that my 3.2GHz processor is 3.1GHz
because 1GHz = 1000 MHz, not 1024 MHz as the code assumes.
Yep, there is no rounding error. BTW if you don't print the number itself (so if it gets converted and is influenced by CONVFMT), you can skip the substitution.
awk 'BEGIN { CONVFMT="%.2f" } /MHz/{ print $4/1024 "GHz" }' /proc/cpuinfo
EDIT:
I realize now that dividing MHz by 1024 is a bit silly. That is just done for bytes, isn't it.
Last edited by Procyon (2009-02-24 01:48:14)
Offline
I realize now that dividing MHz by 1024 is a bit silly. That is just done for bytes, isn't it.
Depends on who you talk to. The trend of hard drive companies over-reporting disk size by using the more traditional even multiples of 1000 led to the creation of so-called "binary multiples".
Offline
Thanks everyone for your help!
I'm going to use the first cut method (because I understand how it works!), but am definitely going to read up on awk because it looks very powerful.
Thanks again!
Offline
Thanks everyone for your help!
I'm going to use the first cut method (because I understand how it works!), but am definitely going to read up on awk because it looks very powerful.
Thanks again!
I wrote an article covering the very basics of sed, awk and cut for Full Circle Magazine (Ubuntu community magazine) if you're interested in having a look at that to get yourself started: http://fullcirclemagazine.org/2009/02/0 … w-finally/
Last edited by lswest (2009-02-25 07:03:39)
Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds
Offline
You'd be surprised at the kind of crazy stuff you can do with sed and awk.
Offline