ksh scripting: Extract 1 most recent record for unique key


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh scripting: Extract 1 most recent record for unique key
# 1  
Old 10-30-2008
Data ksh scripting: Extract 1 most recent record for unique key

I'm loading multiple delimited files into an Oracle DB using sqlldr on Unix. I would like to get only the most recent record per each unique key. There may be multiple updates for each key, but I only want the most recent one.

There is a date column in my delimited files, so I'm using cat to combine multiple files into one file, then I sort that file in descending order by date, using the unix sort command.

When I load with sqlldr, I have a unique key restraint on my Oracle table, so I'm grabbing the first occurrence for each key (the most recent, since they're in *descending* order by date) and throwing away the rest (the earlier out-of-date ones), but it seems to me that it's very inefficient, and it creates a huge amount of "errors" in my sqlldr log file (I don't want to use the SILENT=errors option, or put my errors in /dev/null, since there may be genuine errors that I don't want to miss.)

Is there some way to get those "first" (most recent) occurrences using Unix scripting, instead of doing it in sqlldr?

Thanks,
OP
# 2  
Old 10-30-2008
Well, you didn't supply any sample data so it's difficult to give you a specific answer, but presuming the second field is the unique key, and presuming the fields are separated by white space, this will output only the first record for each key:

Code:
awk '$2 in a { next } { a[$2]; print }' inputfile

# 3  
Old 10-31-2008
Bug

Thanks for the help.

This is the sort instruction I use in my .ksh script (sort by 4th column in descending order):

sort -r -t! +3 -4 unsorted_file.txt -o sorted_file.txt

Here is some mocked up data. This is what it looks like after sorting:

19127171 ! SUCCEEDED ! ACTIVITY-4 ! 2008-10-31 05:58:48.820 ! XYZ !
19127171 ! INPROGRESS ! ACTIVITY-4 ! 2008-10-31 05:58:27.355 ! ABC !
19127171 ! SUCCEEDED ! ACTIVITY-3 ! 2008-10-31 05:58:26.308 ! JKL !
19127171 ! INPROGRESS ! ACTIVITY-3 ! 2008-10-31 05:58:12.482 ! TUV !
19127171 ! SUCCEEDED ! ACTIVITY-2 ! 2008-10-31 05:58:04.668 ! LMN !
19127171 ! INPROGRESS ! ACTIVITY-4 ! 2008-10-31 05:58:03.355 ! FGH !
19127171 ! INPROGRESS ! ACTIVITY-4 ! 2008-10-31 05:57:43.355 ! BCD !
19127171 ! INPROGRESS ! ACTIVITY-3 ! 2008-10-31 05:57:32.381 ! WXY !
19127171 ! SUCCEEDED ! ACTIVITY-1 ! 2008-10-31 05:57:31.266 ! PQR !
19127171 ! INPROGRESS ! ACTIVITY-2 ! 2008-10-31 05:57:21.717 ! PBJ !

The first three columns are my unique key. So, for ACTIVITY-4, I want only the most recent "INPROGRESS" record (line 2), as well as the "SUCCEEDED" record (line 1). The previous "INPROGRESS" records (lines 6,7) can be thrown away. Similarly, for "ACTIVITY-3", keep lines 3 & 4, throw away line 8.

Thanks!
OP
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need code for updating second record to first record in shell scripting

Hi,, I have requirement that i need to get DISTINCT values from a table and if there are two records i need to update it to one record and then need to submit INSERT statements by using the updated value as a parameter. Here is the example follows.. SELECT DISTINCT ID FROM OFFER_GROUP WHERE... (1 Reply)
Discussion started by: Samah
1 Replies

2. Shell Programming and Scripting

Sort Unique Column with the most recent timestamp

Hello, I have a sample file with the below contents : Backup Oracle8_P112_PEGA_Archivedel Completed full 10/11/2015 03:50:06PM Backup Oracle8_G567_PEGA_Archivedel Completed full 10/11/2015 01:15:56PM Backup Oracle8_P112_PEGA_Archivedel Completed full ... (8 Replies)
Discussion started by: rahul2662
8 Replies

3. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

4. Shell Programming and Scripting

How to add trailer record at the end of the flat file in the unix ksh shell scripting?

Hi, How to add trailer record at the end of the flat file in the unix ksh shell scripting can you please let me know the procedure Regards Srikanth (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

5. Shell Programming and Scripting

Grab unique record from different files on a condition

Hi, I think this is the toughest prob :wall: I have ever come across and I thankfully owe all of u for helping me cross this. cat 1.txt cat 2.txt K now. This is what I am looking for. Output.txt Here is how my output has been generated. First, the column one of each file... (6 Replies)
Discussion started by: jacobs.smith
6 Replies

6. Shell Programming and Scripting

recent test -e ksh incompatibility in hpux?

On a very new (11.31) hpux machine, I can no longer execute shell fragements like: if ; then . .profile.foo fi and get "ksh: test: argument expected" if I convert this to -d or -f as appropriate (which I've not had to do on older versions of hpux (11.23) nor any other unix platform... (9 Replies)
Discussion started by: Peeter Joot
9 Replies

7. Shell Programming and Scripting

Managing sequence to make unique record

Hi Everyone, Using shell script i am getting final file as attached below. In this 4th column value should be unique using any sequence. for instance I've 1_13020_SSGM which is appearing 6 times in file and i should change it like 1_13020_SSGM_1,1_13020_SSGM_2,....1_13020_SSGM_6. Can someone... (4 Replies)
Discussion started by: gehlnar
4 Replies

8. Shell Programming and Scripting

Mail cleanup from ksh script, keeping 50 most recent msgs

I found some posts describing how to completely clean out a mailbox in Unix/Linux. But I want to keep the 50 most recent messages. Any ideas out there? Thanks! (3 Replies)
Discussion started by: OPTIMUS_prime
3 Replies

9. Shell Programming and Scripting

minimum number of unique key

input a 1 a 2 a -1 b 1 b 2 b 3 output a -1 b 1 Thanx ---------- Post updated at 09:42 PM ---------- Previous update was at 09:10 PM ---------- Ok I managed it (7 Replies)
Discussion started by: repinementer
7 Replies

10. Shell Programming and Scripting

Simulate ENTER key in ksh scripting

Using SCO 5, Ksh here I am trying to automate some process that i am doing manually. During some point of time, I will have to press enter to clear away some messages. This is how I do manually. *one command* r r r r r I enter the command once, then press enter key 1 more... (6 Replies)
Discussion started by: khaos83_2000
6 Replies
Login or Register to Ask a Question