LinuxQuestions.org
Visit Jeremy's Blog.
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 03-31-2008, 09:30 AM   #1
wolfipa
LQ Newbie
 
Registered: May 2004
Location: Austria
Distribution: Fedora Ubuntu OpenSuse
Posts: 26

Rep: Reputation: 15
calculate totals in shell


Hi,

another question from me.
I have a csv file with e.g. the following values:
Code:
Name;Initials;Zip Code;Totals
Jim;J D;12345;5
Jack;J K;42345;10
Jim;;12345;6
Jack;;42345;11
And I want to create a file with the total sum of "Totals". e.g:
Code:
Name;Totals
Jim;11
Jack;21
Any idea doing this easily?
 
Old 03-31-2008, 09:48 AM   #2
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
One strategy would be:
Sort categories are the text up to the first ;
you need to strip the lines in each category of everything up to and including the third ;
then you can just add the results in each category and cat the finished lines to a new txt file.

Sorted?
Look at grep, sed and awk. Though perl is supposed to be good at this.
You will also have course notes on these.

Alternatively - you can tell us why this is not homework?
 
Old 03-31-2008, 12:37 PM   #3
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 smells like homework, but...
First, this is technically not a CSV file, but still it does use an easily recognizable delimiter, the ';'. So in awk, if one were to make the semicolon the field separator, FS, then for each record, you would simply print $1 & $4. The whole thing is a one-liner.
--- rod.
 
Old 03-31-2008, 03:14 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by theNbomr View Post
then for each record, you would simply print $1 & $4.
It’s a little more complicated than that. For whatever language you use, you will need an associative array (i.e., hash) whose key is $1 and to whose value you add $4 after each line.
 
Old 03-31-2008, 04:57 PM   #5
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
Well, actually, in awk $1 & $4 are all you need to print.
Code:
awk 'BEGIN{ FS=";" } //{print $1";"$4}' wolfipa.dat
(original posters data in 'wolfipa.dat')

I suppose in some sense $1, $2... are associative arrays. But there it is for you.
This is one step above 'hello world' for awk programs.

--- rod.
 
Old 03-31-2008, 05:17 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by theNbomr View Post
Well, actually, in awk $1 & $4 are all you need to print.
Code:
awk 'BEGIN{ FS=";" } //{print $1";"$4}' wolfipa.dat
(original posters data in 'wolfipa.dat')
Except this is not what the OP wants.

Something along the lines of this (if you want to stay with awk) should do it:
Code:
awk '
BEGIN{ FS=";" }
NR==1{ print $1";"$4; next }
{ a[$1]+=$4 }
END{ for (i in a) print i";"a[i] }
' wolfipa.dat
 
Old 03-31-2008, 09:06 PM   #7
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
Oops. Just re-read the OP's question. You are quite right. And I'd say you've seized upon the minimal solution. Still small enough to be a one-liner, IMHO.
--- rod.
 
Old 04-02-2008, 06:12 PM   #8
Simon Bridge
LQ Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 198Reputation: 198
Got that Wolfie? Homework complete
 
  


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
Calculate average from csv file in shell script khairilthegreat Linux - Newbie 5 11-21-2007 12:57 PM
Toggle Post Totals On/Off trickykid LQ Suggestions & Feedback 8 03-15-2005 10:22 PM
shell script to calculate the time a user is logged in racer_mec Linux - Newbie 2 02-08-2005 11:40 AM
Calculate in shell script Ephracis Linux - General 1 12-16-2004 11:11 AM

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

All times are GMT -5. The time now is 06:52 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