The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
awk question..

Hi All,
I need to add up values given by the grep command.Following is the sample:

Code:
[root@iqmango ~]# grep cores /proc/cpuinfo
cpu cores       : 4
cpu cores       : 4
cpu cores       : 4
cpu cores       : 4
cpu cores       : 4
cpu cores       : 4
cpu cores       : 4
cpu cores       : 4
I can do this using a for loop , but wanted to know if this could be done in one line to make the code elegant, maybe using awk or some other tool.

Thanks!
nua7
  #2 (permalink)  
Old 07-17-2008
santyshyam santyshyam is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 6
Hi

If you want to know the no of occurence of cores you can pipe your grep comand to wc -l

ie grep cores /proc/cpuinfo |wc -l



Cheers,
Santy
  #3 (permalink)  
Old 07-17-2008
christineida christineida is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 3
Try This...
grep cores /proc/cpuinfo | awk '{print $4}' | nawk '{sum +=$1} END{print sum}'

Thnx,
Christine Ida
  #4 (permalink)  
Old 07-17-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
This won;t work , because I need to count the total number of cores , including all processors.

Code:
[root@iqmango ~]# grep cores /proc/cpuinfo |wc -l
8
This gives me the total number of processors but not cores.For cores I need to add up the lines as mentioned in the fiorst post.

Hope the confusion is clear.

Thanks!
nua7
  #5 (permalink)  
Old 07-17-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Actually, that's not going to give you the right number, because there is an entry in /proc/cpuinfo for each core. So if you have 2 4-core processors, even though there will be 8 entries in /proc/cpuinfo, each will report that their processor contains 4 cores, and you will get a result of 32 when in fact you only have 8 cores.

santysham's solution will actually give you the correct result, perhaps in a not very obvious way. You can abbreviate it to grep -c cores /proc/cpuinfo.
  #6 (permalink)  
Old 07-17-2008
nua7 nua7 is offline
Registered User
  
 

Join Date: Mar 2008
Location: /bin/sh
Posts: 353
Thumbs up Thanks all for the replies!

Thank you all for the replies!

I had to count the total number of cores for the server.

Christineida's solution works perfect for me..! Thanks a lot christineida!
  #7 (permalink)  
Old 07-17-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
Sure, it works, but the result is wrong! ;-)
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0