LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-20-2010, 08:06 AM   #1
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Rep: Reputation: 0
Script to add up numbers in textfile


All,

I have a textfile that - after parsing a lot - looks like the following:

Mark 3245
Mark 67436
Mark 821
Owen 35
Owen 870
Patrick 6321
Patrick 34
Ruben 47

Result should be:
Mark 71502
Owen 905
Patrick 6355
Ruben 47

I guess this would be doable with an AWK loop where you add up $2 for all lines where $1 is the same...

Any ideas how that would work (or even without awk ofcourse)?

Thanks a lot in advance!

Last edited by philipz; 05-20-2010 at 08:09 AM. Reason: Typo
 
Old 05-20-2010, 08:18 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This is a classic case for the use of associative arrays (language-agnostic example):
Code:
    person{$1} = person{$1} + $2;
Until the first encounter with each 'person' (the key field), the array element does not exist. Thereafter, it exists, has a numeric value, and can be arithmetically manipulated. This kind of code can be used in Awk, Perl, PHP (I think), Python (I think), maybe some others.
--- rod.
 
Old 05-20-2010, 08:26 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk '{_[$1]+=$2}END{for(x in _)print x,_[x]}' file
 
1 members found this post helpful.
Old 05-20-2010, 08:38 AM   #4
philipz
LQ Newbie
 
Registered: Apr 2004
Posts: 25

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Code:
awk '{_[$1]+=$2}END{for(x in _)print x,_[x]}' file
Not sure how it does the job but that works...
Thanks guys for the help, much appreciated!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
[SOLVED] how to find a word in a textfile starting from the BOTTOM of the textfile ? markraem Linux - Software 3 02-08-2010 06:12 AM
interchanging lines in a textfile in bash script neo2k Linux - Newbie 5 07-28-2009 11:48 AM
Help.. how do I add two numbers? Tengil Linux - Newbie 3 03-04-2004 12:58 PM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 09:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration