LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2009, 05:13 AM   #1
ttsdinesh
LQ Newbie
 
Registered: Jun 2009
Location: Erode,TN,India
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 0
Question Parsing the output


I want to display particular line of the output. For eg, i executed "cat /proc/cpuinfo". The output is:
Quote:
[root@Ubuntu]# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 15
model name : Intel(R) Core(TM)2 Duo CPU T5270 @ 1.40GHz
stepping : 13
cpu MHz : 800.000
cache size : 2048 KB
physical id : 0
siblings : 2
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 10
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts pni dtes64 monitor ds_cpl est tm2 ssse3 cx16 xtpr pdcm lahf_lm ida
bogomips : 2791.74
clflush size : 64
I want to display CPU MHz alone. How to parse the Output? Here my CPU MHz is 800.00. I want to assign the value to a variable, say for eg. N, and write the value of N to a file named "cpu.txt". How to perform it? Thanks in advance.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-17-2009, 05:26 AM   #2
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
use some shell scripting...

As an example:

$ grep "cpu MHz" /proc/cpuinfo | awk '{print $4}' | cut -d \. -f 1

will give you the "800".

And, perhaps you'd also like to use bc to round a floating-point MHz value.
 
Old 12-17-2009, 05:30 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If you want the total line (ie: cpu MHz : 800.000): grep "cpu MHz" /proc/cpuinfo

If you only want the number (Ie: 800.000): awk '/cpu MHz/ { print $4 }' /proc/cpuinfo

Assigning it to a variable you can do as follows:

cpuMhz="`awk '/cpu MHz/ { print $4 }' /proc/cpuinfo`"
or
cpuMhz=$(awk '/cpu MHz/ { print $4 }' /proc/cpuinfo)

Writing it to a file (not using the variable): awk '/cpu MHz/ { print $4 }' /proc/cpuinfo > cpu.txt

Writing to a file using the variable: echo $cpuMhz > cpu.txt

Hope this helps.
 
2 members found this post helpful.
Old 12-17-2009, 05:31 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,720

Rep: Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703Reputation: 1703
Code:
awk '{ if ($2=="MHz")print $4 }' /proc/cpuinfo
Or, no decimals
Code:
awk '{ if ($2=="MHz")printf "%.0f\n", $4 }' /proc/cpuinfo
Evo2.
PS. druunas matching method is probably better than the "if" I use here.

Last edited by evo2; 12-17-2009 at 05:34 AM. Reason: PS.
 
Old 12-17-2009, 05:50 AM   #5
ttsdinesh
LQ Newbie
 
Registered: Jun 2009
Location: Erode,TN,India
Distribution: Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 0
Thank u evo2, druuna, HasC. Let me try these snippets and get u later.
 
Old 12-17-2009, 05:53 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by HasC View Post
use some shell scripting...

As an example:

$ grep "cpu MHz" /proc/cpuinfo | awk '{print $4}' | cut -d \. -f 1

will give you the "800".

And, perhaps you'd also like to use bc to round a floating-point MHz value.
useless use of grep and cut
 
Old 12-17-2009, 05:56 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk -F":" 'BEGIN{IGNORECASE=1}/^cpu MHz/{print $2} ' /proc/cpuinfo > file
 
Old 12-17-2009, 06:06 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@ghostdog74: Why the "complicated" awk command (that leaves the tab/space in front of the output)?

Isn't awk '/cpu MHz/ { print $4 }' /proc/cpuinfo the simpler solution.
 
Old 12-17-2009, 06:25 AM   #9
HasC
Member
 
Registered: Oct 2009
Location: South America - Paraguay
Distribution: Debian 5 - Slackware 13.1 - Arch - Some others linuxes/*BSDs through KVM and Xen
Posts: 329

Rep: Reputation: 55
the simplest solution is the one you like the most, I believe.
An Awk guru would use the ghostdog snippet, a Perl monger would program something else (in perl, obviusly :-). I prefer shell scripting
 
Old 12-17-2009, 06:32 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@HasC: Most people tend to go with what they know (hence the cat file | awk 'something' | awk 'somethingelse' constructs, which can probably done with one awk statement).

I noticed over time that ghostdog74 likes resource friendly, elegant and precise coding (which is a good thing!) and I'm a bit confused why he posted that specific command. Especially because the output isn't fully correct.
 
Old 12-17-2009, 06:42 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by druuna View Post
@ghostdog74: Why the "complicated" awk command (that leaves the tab/space in front of the output)?

Isn't awk '/cpu MHz/ { print $4 }' /proc/cpuinfo the simpler solution.
errmm, because you posted this solution earlier than me that's why. As for the "complicated" version, i am being overly cautious, that's all. As for the output, changing -F":" to -F": " will correct that minor space problem.

Last edited by ghostdog74; 12-17-2009 at 06:45 AM.
 
Old 12-17-2009, 06:48 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@ghostdog74: LOL

BTW: The output of /proc/xyz is by convention (the describing part), no need to be overly cautious. But then again, conventions are broken all the time, so maybe it is a good idea to make it case insensitive.....
 
  


Reply

Tags
output, parse


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ps -eH | grep java output in a active passive clustered output johnkalikavunkal Linux - Server 2 01-30-2009 11:21 PM
technique to know usb root hub by parsing lsusb -v command output in SLES10 2.6.16 ke deedhnd Linux - Server 0 05-02-2008 09:06 AM
parsing pages from output of search engine kshkid Programming 11 07-27-2007 08:49 AM
parsing a string from grep output in bash xpromisex Programming 2 11-12-2006 09:12 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration