You are not logged in.

#1 2009-02-23 22:53:06

ilovefridge
Member
Registered: 2007-04-18
Posts: 22

sed help - remove everything after decimal point

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. mad

Thanks!

Last edited by ilovefridge (2009-02-23 22:58:25)

Offline

#2 2009-02-23 23:58:24

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: sed help - remove everything after decimal point

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 wink.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#3 2009-02-24 00:08:24

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: sed help - remove everything after decimal point

I'd use awk:

awk '/MHz/{ sub(/\..*/, "", $4); print $4 }' /proc/cpuinfo

Offline

#4 2009-02-24 00:18:14

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: sed help - remove everything after decimal point

I don't know awk... Should really start learning some sad


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#5 2009-02-24 00:45:47

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: sed help - remove everything after decimal point

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

#6 2009-02-24 01:01:37

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: sed help - remove everything after decimal point

Procyon wrote:

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

#7 2009-02-24 01:16:40

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: sed help - remove everything after decimal point

Procyon wrote:

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

#8 2009-02-24 01:19:45

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: sed help - remove everything after decimal point

skottish wrote:

The top says that my 3.2GHz processor is 3.1GHz

because 1GHz = 1000 MHz, not 1024 MHz as the code assumes.

skottish wrote:

and the bottom one says 321MHz instead 3214MHz.

because the code assumes 3-digit clockspeed.

Offline

#9 2009-02-24 01:36:45

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: sed help - remove everything after decimal point

B wrote:
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.

tam1138 wrote:
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

#10 2009-02-24 01:57:47

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: sed help - remove everything after decimal point

Procyon wrote:

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. smile  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

#11 2009-02-24 13:03:52

ilovefridge
Member
Registered: 2007-04-18
Posts: 22

Re: sed help - remove everything after decimal point

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

#12 2009-02-24 16:15:46

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: sed help - remove everything after decimal point

ilovefridge wrote:

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

#13 2009-02-25 16:36:27

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: sed help - remove everything after decimal point

You'd be surprised at the kind of crazy stuff you can do with sed and awk.

Offline

Board footer

Powered by FluxBB