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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Deleting column from a flatfile with delimiter rsprabha Shell Programming and Scripting 5 1 Week Ago 01:18 AM
Traverse a flatfile and check for errors aravindc Shell Programming and Scripting 1 04-16-2008 11:56 PM
Adding a new field using sed or awk? sasabune UNIX for Dummies Questions & Answers 3 06-01-2007 05:57 PM
encrypting Unix flatfile rkumar28 UNIX for Dummies Questions & Answers 1 04-29-2004 06:31 AM
Finding a column in a flatfile peter.herlihy UNIX for Dummies Questions & Answers 3 01-07-2002 04:08 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-13-2007
Registered User
 

Join Date: Apr 2004
Location: Long Island Ny
Posts: 23
Stumble this Post!
Adding field of flatfile by an index.

Hey guys, I was given a task that involved parcing a large file that looked somthing like this...

A1-0999999,SMITH,.25
A1-0999999,JOHN,.75
A1-0999999,HELMET,.1.25
A1-0999999,HOOP,.10.25
D1-1212121,SMITH,4.00
D1-1212121,TH,9.00
D1-1212121,MITCH,10.20
D1-1212121,RETAL,3.00
A1-9909555,,3.00
E1-099999,,7.00

What the task at hand is, is to Produce a file that looked like this...
A1-0999999,,12.50
D1-1212121,,26.20
A1-9909555,,3.00
E1-099999,,7.00

I am basically condensing by the 1st value and adding up the 3rd field based on a common 1st field. I figured a way to do this using grep, but it was taking way too long. I would like to write an awk script that will add up field three until the 1st field changes, then print the 1st feild and the sum of all the 3rd feilds.

Any help would be greatly appreciated!



Any help would
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-13-2007
Registered User
 

Join Date: Nov 2006
Posts: 23
Stumble this Post!
hi,

PHP Code:
awk -F'{sum[$1]+=$3} END {OFS=",";for (i in sum) print i,","sum[i]}' data 
Thanks
Sumeet
Reply With Quote
  #3 (permalink)  
Old 02-14-2007
Registered User
 

Join Date: Apr 2004
Location: Long Island Ny
Posts: 23
Stumble this Post!
Thank you sumeet!!!!!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0