Matching fields of rows and then operating


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching fields of rows and then operating
# 1  
Old 12-04-2008
MySQL Matching fields of rows and then operating

Hi All,
I was seaching for script for solaris 5.10 environmet to get a output file from Input file like this.
INPUT FILE----------------
1000KE,MINE,74748
1000KE,YOUR,123998
200KE,MINE,886049
50KE,MINE,474176
50KE,YOUR,379998
100KE,YOUR,999994
50KE,MINE,9601
50KE,YOUR,990393
1000KE,MINE,499997
100KE,YOUR,499998
100KE,MINE,999996

And OUTPUT FILE-----------------
1000KE,MINE,(74748+499997)
1000KE,YOUR,74748
200KE,MINE,886049
50KE,MINE,(474176+9601)
50KE,YOUR,(379998+990393)
100KE,YOUR,(999994+499998)
100KE,MINE,999996

LOGIC: Add third field ($3)of all rows and make it single row if 1st and 2nd Field are matching./OR/ Find & match 1st and 2nd filed and merge them with making sum of third field.

Thanks In Advance
Ashis
# 2  
Old 12-04-2008
Use nawk or /usr/xpg4/bin/awk on Solaris.

Code:
awk -F, '{a[$1","$2]+=$3}END{for(i in a){printf("%s,%d\n", i,a[i])}}' file

Regards
# 3  
Old 12-04-2008
awk -F"," '{ my_array[$1","$2]+=$3 } END { for (each_rec in my_array) printf("%s,%s\n", each_rec,my_array[each_rec])}' input_file > output_file
# 4  
Old 12-04-2008
Thanks A Ton!!!

Perfact, That wored perfact,
Thanks a Lot to both of you.
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Matching the rows in 2 files

I have a file like this AFF3 BCL2 AGTRAP BRAF AHRR NCOA2 AKAP9 BRAF And second input file like this chromosome start end gene chr1 38177326 38664955 AFF3 chr4 148077060 148088064 AGTRAP chr13 74211117 74292309 AHRR chr5 3928185 ... (4 Replies)
Discussion started by: raj_k
4 Replies

2. Shell Programming and Scripting

Merge matching rows

Hello, I need this output. thank you very much. input: Code: ***table***wood ***snack***top ***table***garfield ***big***zen ***table***cars output: Code: ***table***wood2345garfield2345cars ***snack***top ***big***zen (7 Replies)
Discussion started by: tara123
7 Replies

3. Shell Programming and Scripting

awk to grep rows by multiple fields

Hello, I met a challenge to extract part of the table. I'd like to grep the first three matches based on field1 and field2. Input: D A 92.85 1315 83 11 D A 95.90 757 28 3 D A 94.38 480 20 7 D A 91.21 307 21 6 D A 94.26 244 ... (6 Replies)
Discussion started by: yifangt
6 Replies

4. Shell Programming and Scripting

Extract fields from different rows.

Hi, I have data like below. SID=D6EB96CC0 HID=9C246D6 CSource=xya Cappe=1 Versionc=3670 MAR1=STL MARS2=STL REQ_BUFFER_ENCODING=UTF-8 REQ_BUFFER_ORIG_ENCODING=UTF-8 RESP_BODY_ENCODING=UTF-8 CON_ID=2713 I want to select CSource=xya (18 Replies)
Discussion started by: chetan.c
18 Replies

5. Shell Programming and Scripting

Remove rows with first 4 fields duplicated in awk

Hi, I am trying to use awk to remove all rows where the first 4 fields are duplicates. e.g. in the following data lines 6-9 would be removed, leaving one copy of the duplicated row (row 5) Borgarhraun FH9822 ol24 FH9822_ol24_m20 ol Deformed c Borgarhraun FH9822 ol24 ... (3 Replies)
Discussion started by: tomahawk
3 Replies

6. Shell Programming and Scripting

Replacing certain fields from certain rows

Hi all, say for example i have the next input file 30 Au 7.500000 7.500000 5.774000 Au 7.500000 8.995000 8.363000 Au 7.500000 6.005000 8.363000 Au 20.633000 7.500000 9.226000 Au 20.632000 6.005000 6.637000 Au 20.632000 ... (4 Replies)
Discussion started by: ezitoc
4 Replies

7. Linux

matching two fields

Hi I am having 2 fields and if f1=f2 i wanna print that line eg 1 2 1 3 1 9 2 2 3 5 9 9 In the abov eg. the highlighted lines shud be printed 2 2 9 9 Thanking u (3 Replies)
Discussion started by: binnybio
3 Replies

8. Shell Programming and Scripting

Matching by key fields

I have a file (key.dat) that contains two columns: AA|1234| BB|567| CC|8910| I have another file (extract.dat) that contains some data: SD|458|John|Smith| AA|3345|Frank|Williams| AA|1234|Bill|Garner| BD|0098|Yu|Lin| BB|567|Gail|Hansen| CC|8910|Ken|Nielsen| I want to compare the... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

9. Shell Programming and Scripting

matching 2 exact fields

Dear experts, I have a file1 that looks like 60127930928 2091 60129382039 2092 60126382937 2091 60128937928 2061 60127329389 2062 60123748730 2061 60128730293 2061 and file 2 that looks like 60127930928 2091 60129382039 2092 60126382937 2093 60128937928 2061 60127329389... (2 Replies)
Discussion started by: aismann
2 Replies

10. Shell Programming and Scripting

Extract duplicate fields in rows

I have a input file with formating: 6000000901 ;36200103 ;h3a01f496 ; 2000123605 ;36218982 ;heefa1328 ; 2000273132 ;36246985 ;h08c5cb71 ; 2000041207 ;36246985 ;heef75497 ; Each fields is seperated by semi-comma. Sometime, the second files is... (6 Replies)
Discussion started by: anhtt
6 Replies
Login or Register to Ask a Question