Adding total of first field for each number in the second field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding total of first field for each number in the second field
# 1  
Old 01-17-2011
Adding total of first field for each number in the second field

Dears,

I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field .
Code:
17215630 ,   0
907043 ,1
201050 ,10
394149 ,4
1964 ,9
17215630, 0
907043 ,1
201050, 10
394149 ,4
1964 ,9
1234234, 55
23 ,100
33 ,67

Shown below is the expected output
Code:
34431260,0
1814086,1
402100,10
788298,4
3928,9
1234234, 55
23 ,100
33 ,67

Please let me know how can I achieve this.

Regards,
XYZ

Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!
# 2  
Old 01-17-2011
Unsorted:
Code:
 awk -F\, '{a[$2]+=$1}END{for (i in a ){printf("%d ,%d\n", a[i],i)}}'  file

# 3  
Old 01-17-2011
Code:
 ruby -F"," -ane 'BEGIN{h={};h.default=0};h[$F[1].to_i]+=$F[0].to_i;END{h.each_pair{|x,y|puts "#{y},#{x}\n" } }' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate total and percent off field in file

Trying to use awk to print the lines in file that have either REF or SNV in $3, add a header line, sort by $4 in numerical order. The below code does that already, but where I am stuck is on the last part where the total lines are counted and printed under Total_Targets, under Targets_less_than is... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Adding field to file

Hi I have file as given below 040|14300|40.0|563000 042|13200000|40.0 041|100|40.0 043|10000|40.0 045|102|40.0 I want to check if field 4 is null then I have to place | in position 4. result set should be 040|14300|40.0|563000 042|13200000|40.0| 041|100|40.0| 043|10000|40.0|... (9 Replies)
Discussion started by: shabeena
9 Replies

3. Shell Programming and Scripting

Calculate the total 4 field based on the conditions

Please help me to write a script Match with ACNO & NAME if it matched calculate the total val1 val2 val3 and val4 and GT is total of ACNO wise.please check the output Table ----------------- 1005|ANDP|ACN|20|50|10|30 1005|ANDP|ACN|20|10|30|40 1001|AND|NAC|40|50|40|50... (22 Replies)
Discussion started by: kalia4u
22 Replies

4. Shell Programming and Scripting

How to add subtotal and total according 3rd field mentioned below table?

111AKKK|SHA|20|25 111|AKKK|SHA|15|35 112|ABC|FL|25|45 112|ABC|FL|15|15 115|AKKK|ASH|10|15 115|AKKK|ASH|20|20 112|ABC|FL|25|20 115|AKKK|ASH|30|35 111|AKKK|SHA|10|45 112|ABC|KL|15|15 112|ABC|KL|20|25 115|AKKK|ASH|30|35 please write a shell script output should be below mentioned... (26 Replies)
Discussion started by: udhal
26 Replies

5. Shell Programming and Scripting

Adding matching field

I have a flat file test.log red,5,,,,, green,7,,,,, blue,4,,,,, red,8,,,,, green,9,,,,, How i get a a result: blue,4,,,,, green,16,,,,, red,13,,,,, Thanks Video tutorial on how to use code tags in The UNIX and Linux Forums. (2 Replies)
Discussion started by: sabercats
2 Replies

6. Shell Programming and Scripting

Adding new field

Hello, I have a main file with IP addresses like this: Erisim var,100,172.17.241.5,4006,60,IS0799,TCP/IP Erisim var,1003,172.17.140.4,4004,60,IS2156,TCP/IP Erisim var,1004,172.17.140.5,4002,60,IS2636,TCP/IP Erisim var,1005,172.17.140.5,4004,60,IS2436,TCP/IP Erisim... (8 Replies)
Discussion started by: Spunkerspawn
8 Replies

7. Shell Programming and Scripting

adding field values if field matches

hi i have file as below , i want to add duplicate records like bell_bb to one record with valuve as 15 ( addition of both ) any oneline awk script to achive this ? header 0 CAMPAIGN_NAME 1 Bell_BB 14 Bell_MONTHLY 803 SOLO_UNBEATABLE 644 Bell_BB 1 Bell_MONTHLY 25 SOLO_UNBEATABLE... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

8. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

9. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

10. Shell Programming and Scripting

Get the total of a field in all the lines of a group

Hi I have a Fixed format data file where I need to to get the total of the field at certain position in a file for a group of lines. In this data file I need the total of all the field ats position 30:39 for each line starting with 6 and for each group startign with 5. Which means for... (27 Replies)
Discussion started by: appsguy616
27 Replies
Login or Register to Ask a Question