LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 04-21-2004, 06:39 AM   #1
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Rep: Reputation: 0
Add values using awk


Hi,
I have this file:
Anni;2
Arna;9
Anni;4
Ally;1
Ally;1

I want the output to be:
Anni has: 6
Arna has: 9
Ally has: 2

This is my code so far:
awk -F';' { count[$1]++ } END { for (w in count) print w "has: " }' myFile

How can I find the sum of $2 for each name? I have tried several for loops and incremental variables, but I doesn't seem to work for me.

I appreciate any help!
- Helene
 
Old 04-21-2004, 10:50 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Code:
  cat file | sed 's/;/ has: /g' > newfile
It isn't awk but it should work as you wanted.
 
Old 04-21-2004, 11:21 AM   #3
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
sort file | awk -F\; '($1 != prev && NR != 1) {print prev" has "sum; sum=0} {sum+=$2; prev = $1}END{print $1" has "sum}'
 
Old 04-21-2004, 11:24 AM   #4
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
awk -F';' '{ count[$1]+=$2 } END { for (w in count) print w " has: "count[w] }' file
 
Old 04-21-2004, 06:30 PM   #5
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks for help! I'll use the last one which was the one closest to my way of doing it
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
/proc/stat values don't add up ! mike0w Linux - General 0 10-25-2005 05:01 AM
Sync Values waslit Linux - Software 0 06-05-2004 07:34 AM
rx/tx values Nauseous *BSD 1 05-26-2004 02:06 PM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM
useradd with default values ?! realos Linux - Newbie 1 08-09-2002 07:38 AM


All times are GMT -5. The time now is 09:30 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration