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
du total C3000 UNIX for Dummies Questions & Answers 9 12-05-2008 11:04 AM
total occ. gander_ss Shell Programming and Scripting 6 05-09-2008 04:54 AM
grep running total/ final total across multiple files MrAd UNIX for Dummies Questions & Answers 5 05-08-2007 02:03 PM
How to calculate with awk whatisthis Shell Programming and Scripting 4 11-09-2005 12:39 PM
Total ram orca UNIX for Dummies Questions & Answers 4 04-25-2002 08:19 AM

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 08-21-2008
senthilkumar_ak senthilkumar_ak is offline
Registered User
  
 

Join Date: Aug 2008
Location: Chennai
Posts: 43
Awk help needed to calculate total

Hi all,

I have a flat file like

10 steven
25 mike
47 Charles
127 Nancy
34 steven
23 mike
67 Charles
7761 Nancy
8 steven
54 mike
88 Charles
1267 Nancy

I need to calculate the total of steven and all the members , for this I am using like

grep "`sed -n 1p patterns.txt`" temp.log | awk '{x+=$1}END{print "Total "x}'

where patterns.txt contains
steven
mike
Charles
Nancy

I think i'm using wrong method, is there any other method is available which can do this job very easily.

Regards,
senthilkumar ak
  #2 (permalink)  
Old 08-21-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431

Code:
#  awk '{t[$2]+=$1}END{for (i in t){print i,t[i]}}' file
Nancy 9155
Charles 202
steven 52
mike 102

  #3 (permalink)  
Old 08-21-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,407
Quote:
Originally Posted by Tytalus View Post
Code:
#  awk '{t[$2]+=$1}END{for (i in t){print i,t[i]}}' file
Nancy 9155
Charles 202
steven 52
mike 102
you can't get simpler than this
great work..
  #4 (permalink)  
Old 08-21-2008
yahyaaa's Avatar
yahyaaa yahyaaa is offline
Registered User
  
 

Join Date: May 2008
Location: Jordan
Posts: 89
first sort them by the second field.
then do

nawk 'NR==1{v=$2;Total=0}$1!=v{print v" Total=" Total} ;Total=0;v=$2}$2==v{Total=Total+$1}' input > Total

This should do it.

Regards
  #5 (permalink)  
Old 08-21-2008
yahyaaa's Avatar
yahyaaa yahyaaa is offline
Registered User
  
 

Join Date: May 2008
Location: Jordan
Posts: 89
Quote:
Originally Posted by Tytalus View Post
Code:
#  awk '{t[$2]+=$1}END{for (i in t){print i,t[i]}}' file
Nancy 9155
Charles 202
steven 52
mike 102
Dear Tytalus,

how does this code know the value of i in the END braces.. is it by default ?

Regards
  #6 (permalink)  
Old 08-21-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,407
Quote:
Originally Posted by yahyaaa View Post
Dear Tytalus,

how does this code know the value of i in the END braces.. is it by default ?

Regards
here t is a array so i reads from array t till it reaches the end..
so i don't think you need a value for i..
its called iterating over all the indices of an array..
  #7 (permalink)  
Old 08-21-2008
yahyaaa's Avatar
yahyaaa yahyaaa is offline
Registered User
  
 

Join Date: May 2008
Location: Jordan
Posts: 89
Thank you very very much..
Closed Thread

Bookmarks

Tags
awk calculation

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 01:11 AM.


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