The UNIX and Linux Forums  

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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
sum total by column thh Shell Programming and Scripting 5 11-25-2007 02:41 AM
Finding the most common entry in a column Donkey25 Shell Programming and Scripting 12 11-22-2007 12:16 PM
grep running total/ final total across multiple files MrAd UNIX for Dummies Questions & Answers 5 05-08-2007 02:03 PM
Finding a column in a flatfile peter.herlihy UNIX for Dummies Questions & Answers 3 01-07-2002 08:08 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-20-2008
ironhead3fan ironhead3fan is offline
Registered User
  
 

Join Date: Apr 2008
Location: Maryland
Posts: 3
Finding the total of a column using awk

Here is my file name countries

USSR 8650 262 Asia
Canada 3852 24 North America
China 3692 866 Asia
USA 3615 219 North America
Brazil 3286 116 South America
India 1269 637 Asia
Argentina 1072 26 South America
Sudan 968 19 Africa
Algeria 920 18 Africa

Assuming the second column is the total area, how would I total that column? Many thanks.
  #2 (permalink)  
Old 04-20-2008
ironhead3fan ironhead3fan is offline
Registered User
  
 

Join Date: Apr 2008
Location: Maryland
Posts: 3
Forgot to mention that it can only be for all North American countries
  #3 (permalink)  
Old 04-21-2008
TheOther1 TheOther1 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 3

Code:
area=0
total=0
for area in `grep "North America" countries | awk '{print $2}'`
     do total=$(($total+$area))
     done
echo $total

Example:

Code:
monitor:/tmp# area=0; total=0; for area in `grep "North America" countries | awk '{print $2}'`; do total=$(($total+$area)); done; echo $total
7467

HTH!

Last edited by TheOther1; 04-21-2008 at 12:18 AM..
  #4 (permalink)  
Old 04-21-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,441
using awk..

Code:
awk '/North America/{s+=$2}END{print s}' data.file


Last edited by danmero; 04-21-2008 at 02:16 AM..
  #5 (permalink)  
Old 04-21-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,092

Code:
awk '{n=n+$2}END{print n}' filename

  #6 (permalink)  
Old 04-21-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,345
summer_cherry,

The OP only wants to see the result for North America.

Best regards.
  #7 (permalink)  
Old 01-30-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,893
Similar, but use associative arrays to store the values.

Code:
awk ' { sum[$2]+=$3 } END { for (j in sum) print j,sum[j]; }'

Closed Thread

Bookmarks

Tags
statistical calculator, sum, sum by column, summing group

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:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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