getting CPU Frequency via /proc/cpuinfo

45 views
Skip to first unread message

Patrick Pippen

unread,
Apr 4, 2008, 11:35:01 PM4/4/08
to Central West End Linux Users Group
I'm trying to have a sed script get the cpu frequency from /proc/
cpuinfo.
I know there are other applications that could possibly do the job,
but I need to get it from cpuinfo.

This is how I think it should be done:
sed 's/^cpu Mhz\\s\\+: \\(.\\+\\)\\..*$/\\1/p' /proc/cpuinfo

And this returns: processor: 0
I don't see how the ^cpu Mhz regex matches processor.
I thought my regex-fu was stronger that this maybe I was wrong.

Maybe it's a linux emulation issue that I'm not aware of.

Sean Burns

unread,
Apr 5, 2008, 12:04:34 AM4/5/08
to cwe...@googlegroups.com

I don't think it's a linux emulation thing. Also, I'm by no means
a regex person, but what about plain grep and/+ awk?

$ grep "cpu MHz" /proc/cpuinfo
cpu MHz : 2992.845

$ grep "cpu MHz" /proc/cpuinfo | awk '{print $4}'
2992.845

Sean
--
Fri, 04 Apr 2008 23:01:38 -0500

Eric Wilson

unread,
Apr 5, 2008, 12:12:16 AM4/5/08
to cwe...@googlegroups.com
This line is a little inefficient, as it spawns two processes to do what
a single awk can do intrinsically:

> $ grep "cpu MHz" /proc/cpuinfo | awk '{print $4}'
> 2992.845

i.e. $ awk -F':' '$1 ~ /MHz/ {print $2}' /proc/cpuinfo

Cheers;

E!

> This email was Anti Virus checked by Astaro Security Gateway.
> http://www.astaro.com
>
>
>

This email was Anti Virus checked by Astaro Security Gateway. http://www.astaro.com

Eric Wilson

unread,
Apr 4, 2008, 11:50:57 PM4/4/08
to cwe...@googlegroups.com
Are you looking for just the raw freq?

If so, what about:

$ awk -F':' '$1 ~ /MHz/ {print $2}' /proc/cpuinfo

Cheers;

E!


>


> I'm trying to have a sed script get the cpu frequency from /proc/ cpuinfo.
> I know there are other applications that could possibly do the job, but
I need to get it from cpuinfo.
>
> This is how I think it should be done:
> sed 's/^cpu Mhz\\s\\+: \\(.\\+\\)\\..*$/\\1/p' /proc/cpuinfo
>
> And this returns: processor: 0
> I don't see how the ^cpu Mhz regex matches processor.
> I thought my regex-fu was stronger that this maybe I was wrong.
>
> Maybe it's a linux emulation issue that I'm not aware of.
>
> >
>

Patrick Pippen

unread,
Apr 5, 2008, 12:47:26 AM4/5/08
to cwe...@googlegroups.com
Thanks Eric and Sean for such a fast response.
guess, I should learn when to sed and when not to sed.

> $ awk -F':' '$1 ~ /MHz/ {print $2}' /proc/cpuinfo

> grep "cpu MHz" /proc/cpuinfo | awk '{print $4}'

--
you must know the ledge of wise and dome to understand your culture of freedom!

http://pxpippen.blogspot.com/
http://groups.google.com/group/lispstl

Powerfull Allah

Robert Citek

unread,
Apr 5, 2008, 12:52:19 AM4/5/08
to cwe...@googlegroups.com
On Fri, Apr 4, 2008 at 10:35 PM, Patrick Pippen <daBitt...@gmail.com> wrote:
> I'm trying to have a sed script get the cpu frequency from /proc/
> cpuinfo.
> I know there are other applications that could possibly do the job,
> but I need to get it from cpuinfo.
>
> This is how I think it should be done:
> sed 's/^cpu Mhz\\s\\+: \\(.\\+\\)\\..*$/\\1/p' /proc/cpuinfo

One way to do this with sed:

$ sed -rne '/^cpu MHz/s/^.*: (.*)\..*$/\1/p' /proc/cpuinfo

Another:

$ sed -ne '/^cpu MHz/{s/^.*: //;s/\..*$//;p}' /proc/cpuinfo

And then there's the xml way (sort of):

$ sudo lshw -xml |
xmlstarlet sel -t \
-m "/node/node[@id='core']/node[@id='cpu']/capacity" \
-v "concat(.,' ',@units)" -n

Regards,
- Robert

Patrick Pippen

unread,
Apr 5, 2008, 1:03:36 AM4/5/08
to cwe...@googlegroups.com
On Fri, Apr 4, 2008 at 11:52 PM, Robert Citek <robert...@gmail.com> wrote:

> $ sed -rne '/^cpu MHz/s/^.*: (.*)\..*$/\1/p' /proc/cpuinfo
>
> Another:
>
> $ sed -ne '/^cpu MHz/{s/^.*: //;s/\..*$//;p}' /proc/cpuinfo

Works like a charm with sed -nE '-)

Eric Wilson

unread,
Apr 5, 2008, 1:15:50 AM4/5/08
to cwe...@googlegroups.com
Robert:

Nice - I would not have thought to use sed to both parse and edit out the
remainder.

Very efficient.

Cheers;

E!

Sean Burns

unread,
Apr 5, 2008, 3:01:00 AM4/5/08
to cwe...@googlegroups.com
On Fri, Apr 04, 2008 at 11:12:16PM -0500, Eric Wilson wrote:
>
> This line is a little inefficient, as it spawns two processes to do what
> a single awk can do intrinsically:
>
> > $ grep "cpu MHz" /proc/cpuinfo | awk '{print $4}'
> > 2992.845
>
> i.e. $ awk -F':' '$1 ~ /MHz/ {print $2}' /proc/cpuinfo

Cool. Learn something new everyday!

Sean
--
Sat, 05 Apr 2008 02:00:22 -0500

Reply all
Reply to author
Forward
0 new messages