Sum a column value based on multiple keys


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum a column value based on multiple keys
# 1  
Old 03-24-2010
Sum a column value based on multiple keys

Hi,

I have below as i/p file:

Code:
5ABC    36488989    K       000010000ASB                BYTRES
5PQR    45757754    K       000200005KPC                HGTRET
5ABC    36488989    K       000045000ASB                HGTRET
5GTH    36488989    K       000200200ASB                BYTRES
5FTU    45757754    K       000003002KPC                HGTRET
5FTU    45757754    K       007000001KPC                BYTRES
5GTH    36488989    K       062000100ASB                HGTRET
5PQR    45757754    K       000500000KPC                HGTRET

My o/p should be

Code:
5ABC    36488989    K       000055000ASB 
5GTH    36488989    K       062200300ASB 
5FTU    45757754    K       007003003KPC
5PQR    45757754    K       000700005KPC

Field in green - Key 1
Field in Red - key 2

For all the records having unique combination of key1 and key2, I need to add values in green.
For example I have below two rows for the combination of 36488989 and ABC. In the o/p I need
Code:
5ABC    36488989    K       sum(000010000 + 000045000)

Problem for me here is two keys to get the duplicate rows.
I am trying to use awk to get the solution.

Last edited by Franklin52; 03-24-2010 at 10:15 AM.. Reason: Please use code tags!
# 2  
Old 03-24-2010
Quote:
Originally Posted by nirnkv
I am trying to use awk to get the solution.
Where did you get stuck with your code?
# 3  
Old 03-24-2010
Assuming you meant you want to use awk not trying to use awk
And
You might need to modify the code if your first character starts other than 5 in first column and if the third column is not k...

Code:
awk '{v=substr($1,2);F[v" "$2]+=substr($4,1,9);FF[v" "$2]=substr($4,10)}END{
for(i in F){split(i,A," ");printf("5%s %d K %09d%s \n"),A[1],A[2],F[i],FF[i]}}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum of a column as new column based on header in a script

Hello, I am trying to store sum of a column as a new column inside a file but have to find the column names dynamically I/p c1,c2,c3,c4,c5 10,20,30,40,50 20,30,40,50,60 If i want to find sum only column c1, c3 and output it as c6,c7 O/p c1,c2,c3,c4,c5,c6,c7 10,20,30,40,50,30,70... (6 Replies)
Discussion started by: mkathi
6 Replies

2. Shell Programming and Scripting

Combine multiple rows based on selected column keys

Hello I want to collapse a file with multiple rows into consolidated lines of entries based on selected columns as the 'key'. Example: 1 2 3 Abc def ghi 1 2 3 jkl mno p qrts 6 9 0 mno def Abc 7 8 4 Abc mno mno abc 7 8 9 mno mno abc 7 8 9 mno j k So if columns 1, 2 and 3 are... (6 Replies)
Discussion started by: linuxlearner123
6 Replies

3. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

4. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

5. Shell Programming and Scripting

Find All duplicates based on multiple keys

Hi All, Input.txt 123,ABC,XYZ1,A01,IND,I68,IND,NN 123,ABC,XYZ1,A01,IND,I67,IND,NN 998,SGR,St,R834,scot,R834,scot,NN 985,SGR0399,St,R180,T15,R180,T1,YY 985,SGR0399,St,R180,T15,R180,T1,NN 985,SGR0399,St,R180,T15,R180,T1,NN 2943,SGR?99,St,R68,Scot,R77,Scot,YY... (2 Replies)
Discussion started by: unme
2 Replies

6. UNIX for Dummies Questions & Answers

Sum based on certain column

I have file 1 1/1/2013 A 553.0763397 96 16582 X1 X3 X5 X7 X9 1/1/2013 B 600.8333588 195 11992 X2 X3 X6 X7 X9 1/1/2013 B 459.8333588 195 11992 X1 X3 X6 X7 X9 1/2/2013 A 844.2973022 306 19555 X1 ... (12 Replies)
Discussion started by: radius
12 Replies

7. UNIX for Dummies Questions & Answers

Sum based on column 1

i have file input aaa ccc,45567,rterw,1 bbb dcs,564543,hjghgh,1 aaa ccc,454,rterw,6 i want to sum based on column 1 expected output aaa ccc,7 bbb dcs,1 (4 Replies)
Discussion started by: radius
4 Replies

8. Shell Programming and Scripting

Sum Of Column Based On Column Condition

I have a following inputfile MT,AP,CDM,TTML,MUM,GS,SUCC,3 MT,AP,CDM,TTSL,AP,GS,FAIL,9 MT,AP,CDM,RCom,MAH,GS,SUCC,3 MT,AP,CDM,RTL,HP,GS,SUCC,1 MT,AP,CDM,Uni,UPE,GS,SUCC,2 MT,AP,CDM,Uni,MUM,GS,SUCC,2 TTSL,AP,GS,MT,MAH,CDM,SUCC,20 TTML,AP,GS,MT,MAH,CDM,FAIL,10... (2 Replies)
Discussion started by: siramitsharma
2 Replies

9. Shell Programming and Scripting

Compare files column to column based on keys

Here is my situation. I need to compare two tab separated files (diff is not useful since there could be known difference between files). I have found similar posts , but not fully matching.I was thinking of writing a shell script using cut and grep and while loop but after going thru posts it... (2 Replies)
Discussion started by: blackjack101
2 Replies

10. Shell Programming and Scripting

sum multiple columns based on column value

i have a file - it will be in sorted order on column 1 abc 0 1 abc 2 3 abc 3 5 def 1 7 def 0 1 -------- i'd like (awk maybe?) to get the results (any ideas)??? abc 5 9 def 1 8 (2 Replies)
Discussion started by: jjoe
2 Replies
Login or Register to Ask a Question