verifying column2 for same kind of records and adding corresponding values in column3


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting verifying column2 for same kind of records and adding corresponding values in column3
# 1  
Old 06-09-2009
verifying column2 for same kind of records and adding corresponding values in column3

Hi

am having a file which looks like this
Quote:
07-Jun-2009 919448010059 54 lessrv1
07-Jun-2009 919449829088 52 lessrv1
07-Jun-2009 919449829088 48 lessrv6
07-Jun-2009 919447446632 57 lessrv1
07-Jun-2009 919449011866 61 lessrv1
07-Jun-2009 919447001101 40 lessrv1
07-Jun-2009 919448010062 51 lessrv1
07-Jun-2009 919448010062 58 lessrv7
07-Jun-2009 919448010151 61 lessrv1
07-Jun-2009 919490123007 56 lessrv1
07-Jun-2009 919448010151 39 lessrv4
07-Jun-2009 919448010062 41 lessrv3
i want to get unique values in column2 and sum up the corresponding column3 values and discard the column4 and then write the output in different file.

i.e the output has to be like
Quote:
07-Jun-2009 919448010059 54
07-Jun-2009 919449829088 100
07-Jun-2009 919447446632 57
07-Jun-2009 919449011866 61
07-Jun-2009 919447001101 40
07-Jun-2009 919448010062 150
07-Jun-2009 919448010151 100
07-Jun-2009 919490123007 56
i.e
07-Jun-2009 919449829088 52 lessrv1
07-Jun-2009 919449829088 48 lessrv6

leads to
07-Jun-2009 919449829088 100

thanks in advance
# 2  
Old 06-09-2009
awk '{arr[$1 " " $2] += $3} END {for ( i in arr ) {print i " " arr[i]}' logfile
# 3  
Old 06-09-2009
Hi

Got the answer....its nice. am getting more eagerness to master this art of shell scripting...it does everything u want in a single line itself.

by the way can u explain what the line does...
Code:
awk '{arr[$1 " " $2] += $3} END

its better to get it clarified ..i may misunderstood

With lot of thanks
Aemu
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Subtract values based on records

Hi Guys, I am having below tables in oracle T1 ID F_TYPE F_AMT DATE_COL 1 F 6 11-Feb-16 1 D 2 11-Feb-16 1 D 2 11-Feb-16 1 F 6 11-Feb-16 1 F 2 12-Mar-16 1 D 3 12-Mar-16 1 F 4 10-Apr-16 1 F 4 11-Apr-16 1 D 1 11-Apr-16 T2 ID START_DATE END_DATE F_ID FLAG... (0 Replies)
Discussion started by: rohit_shinez
0 Replies

2. UNIX for Dummies Questions & Answers

Count occurrence of column3 in column2

count occurrence of column3 in column2 with awk Input: AA BB BB AA BB CC AA BB CC AA CC BB CC CC BB BB Output: AA BB 3 (1 Reply)
Discussion started by: aydj
1 Replies

3. Shell Programming and Scripting

Copy header values into records

I'm using a shell script to manipulate a data file. I have a large file with two sets of data samples (tracking memory consumption) taken over a long period of time, so I have many samples. The problem is that all the data is in the same file so that each sample contains two sets of data.... (2 Replies)
Discussion started by: abercrom
2 Replies

4. Shell Programming and Scripting

adding line number to *end* of records in file

Given a file like this: abc def ghi I need to get to somestandardtext abc1 morestandardtext somestandardtext def2 morestandardtext somestandardtext ghi3 morestandardtext Notice that in addition to the standard text there is the line number added in as well. What I conceived is... (4 Replies)
Discussion started by: edstevens
4 Replies

5. Shell Programming and Scripting

Adding Header and Trailer records to a appended file

How can we a shell script and pass date parameters .I have 3 files comming from Datastage with |" delimited I need append 3 files as above: File1: P0000|"47416954|"AU|"000|"INS|"0000|"|"20060601|"99991231|"|"|"|"|"01 File 2:... (2 Replies)
Discussion started by: e1994264
2 Replies

6. Shell Programming and Scripting

Adding existing set of records in the same file

I have a file with 50,000 records in it, i have a requirement to use the same 50,000 records and add them 4 times to the same file to make a total of 200,000 records. I was wondering how to do this using ksh. Any help is greatly appreciated. (2 Replies)
Discussion started by: vpv0002
2 Replies

7. Shell Programming and Scripting

Verifying table records

Hi, Script copies records of two tables into another tables for backup before using oracle's import utility to restore from backup. Now, suppose if the import utility fails then the script will again copy those records from the backup table to the original ones. How to check whether all... (2 Replies)
Discussion started by: milink
2 Replies

8. Shell Programming and Scripting

Adding values concatenating values

I have the following script in a shell # The start of a filename file=$(ls -tr $EMT*.dat | tail -1) # Select the latest file echo $file file_seq=$( < /u02/sct/banner/bandev2/xxxxxx/misc/EFTSQL.dat) echo $file_seq file2 = '$file_seq + 1' echo $file2 It is reading a file EFTSQL.dat... (3 Replies)
Discussion started by: rechever
3 Replies

9. UNIX for Dummies Questions & Answers

Adding SPF records......please help

Does anybody know who I can set up SPR records? I've created a record and it says that I need to and I logged into the server as the root users via ssh and typed in /var/named (which my host told me to do) but all it said was I really need to add this because Microsoft is blocking emails... (11 Replies)
Discussion started by: thehaapyappy
11 Replies
Login or Register to Ask a Question