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
Adding the values of two file Shell_Learner Shell Programming and Scripting 3 04-06-2009 05:59 AM
adding values with a loop hcclnoodles Shell Programming and Scripting 1 05-27-2008 07:42 AM
Adding SPF records......please help thehaapyappy UNIX for Dummies Questions & Answers 11 05-14-2008 06:20 AM
Generating key values for leader records ajfaq UNIX for Dummies Questions & Answers 1 09-20-2007 09:21 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 06-09-2009
aemunathan aemunathan is offline
Registered User
  
 

Join Date: May 2008
Posts: 76
verifying column2 for same kind of records and adding corresponding values in column3

Hi

am having a file which looks like this
Quote:
07-Jun-2009 919448010059 54 lessrv1
07-Jun-2009 919449829088 52 lessrv1
07-Jun-2009 919449829088 48 lessrv6
07-Jun-2009 919447446632 57 lessrv1
07-Jun-2009 919449011866 61 lessrv1
07-Jun-2009 919447001101 40 lessrv1
07-Jun-2009 919448010062 51 lessrv1
07-Jun-2009 919448010062 58 lessrv7
07-Jun-2009 919448010151 61 lessrv1
07-Jun-2009 919490123007 56 lessrv1
07-Jun-2009 919448010151 39 lessrv4
07-Jun-2009 919448010062 41 lessrv3
i want to get unique values in column2 and sum up the corresponding column3 values and discard the column4 and then write the output in different file.

i.e the output has to be like
Quote:
07-Jun-2009 919448010059 54
07-Jun-2009 919449829088 100
07-Jun-2009 919447446632 57
07-Jun-2009 919449011866 61
07-Jun-2009 919447001101 40
07-Jun-2009 919448010062 150
07-Jun-2009 919448010151 100
07-Jun-2009 919490123007 56
i.e
07-Jun-2009 919449829088 52 lessrv1
07-Jun-2009 919449829088 48 lessrv6

leads to
07-Jun-2009 919449829088 100

thanks in advance
  #2 (permalink)  
Old 06-09-2009
baruchgu baruchgu is offline
Registered User
  
 

Join Date: May 2008
Posts: 13
awk '{arr[$1 " " $2] += $3} END {for ( i in arr ) {print i " " arr[i]}' logfile
Bits Awarded / Charged to baruchgu for this Post
Date User Comment Amount
06-09-2009 aemunathan great 50,000
  #3 (permalink)  
Old 06-09-2009
aemunathan aemunathan is offline
Registered User
  
 

Join Date: May 2008
Posts: 76
Hi

Got the answer....its nice. am getting more eagerness to master this art of shell scripting...it does everything u want in a single line itself.

by the way can u explain what the line does...

Code:
awk '{arr[$1 " " $2] += $3} END

its better to get it clarified ..i may misunderstood

With lot of thanks
Aemu
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:48 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