Finding the Latest record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding the Latest record
# 1  
Old 04-18-2014
Finding the Latest record

Dear All,

I have getting data as follows, the second field signifies table name and last one is time stamp. I have return always latest record based on time stamp. Could you please help me ?

I/P
====

Code:
5460-DataAcqu-MF-Extract|bill_cycle_code|SW25|?|?|?|FEDWM708|SALES|MF-Extract/ftp|?|Refresh|M|1st00:15|acqxmain|sw25_20060901_000521|fedex5|184360|2006-09-0100:10:51
5460-DataAcqu-MF-Extract|bill_cycle_code|SW25|?|?|?|FEDWM708|BILCYCLE|MF-Extract/ftp|?|Refresh|M|1st00:15|acqxmain|SW25_20140401_000520|fedex5|184360|2014-04-0105:32:58
5460-DataAcqu-MF-Extract|cash_only_reason_code|SW21|?|?|?|FEDWM704|SALES|MF-Extract/ftp|?|Refresh|D|1st00:15|acqxmain|sw21_20060901_000559|fedex5|184360|2006-09-0100:11:17
5460-DataAcqu-MF-Extract|cash_only_reason_code|SW21|?|?|?|FEDWM704|CASHONLY|MF-Extract/ftp|?|Refresh|D|1st00:15|acqxmain|sw21_20140401_000549|fedex5|184360|2014-04-0105:33:09
5460-DataAcqu-MF-Extract|ctry_x_lang|CTLG|?|?|?|FEDWM056|FEDW056|MF-Extract/ftp|?|Refresh|M|1stat09:00|acqxmain|CTLG_20140401_090032|fedex5|490350|2014-04-0114:06:13
5460-DataAcqu-MF-Extract|edw_domain|DOMN|?|?|?|FEDWD041|FEDW041|MF-Extract/ftp|?|Refresh|W|Sun9:00|acqxmain|DOMN_fix_20031005|fedex5;fedex7|473901|2006-09-1709:08:36
5460-DataAcqu-MF-Extract|edw_domain|DOMN|?|?|?|FEDWW120|FEDW078|MF-Extract/ftp|?|Refresh|W|Sun9:00|acqxmain|domn_20140415_134516|fedex5;fedex7|473901|2014-04-1519:49:19
5460-DataAcqu-MF-Extract|sic_code|SW23|?|?|?|FEDWM706|FEDW255|MF-Extract/ftp|?|Refresh|M|1st00:15|acqxmain|SW23_20140401_000515|fedex5|184360|2014-04-0105:32:07
5460-DataAcqu-MF-Extract|sic_code|SW23|?|?|?|FEDWM706|SALES|MF-Extract/ftp|?|Refresh|M|1st00:15|acqxmain|SW23_20060901_000521|fedex5|184360|2006-09-0100:10:11

O/P
=====
Code:
5460-DataAcqu-MF-Extract|bill_cycle_code|SW25|?|?|?|FEDWM708|BILCYCLE|MF-Extract/ftp|?|Refresh|M|1st00:15|acqxmain|SW25_20140401_000520|fedex5|184360|2014-04-0105:32:58
5460-DataAcqu-MF-Extract|cash_only_reason_code|SW21|?|?|?|FEDWM704|CASHONLY|MF-Extract/ftp|?|Refresh|D|1st00:15|acqxmain|sw21_20140401_000549|fedex5|184360|2014-04-0105:33:09
5460-DataAcqu-MF-Extract|ctry_x_lang|CTLG|?|?|?|FEDWM056|FEDW056|MF-Extract/ftp|?|Refresh|M|1stat09:00|acqxmain|CTLG_20140401_090032|fedex5|490350|2014-04-0114:06:13
5460-DataAcqu-MF-Extract|edw_domain|DOMN|?|?|?|FEDWW120|FEDW078|MF-Extract/ftp|?|Refresh|W|Sun9:00|acqxmain|domn_20140415_134516|fedex5;fedex7|473901|2014-04-1519:49:19
5460-DataAcqu-MF-Extract|sic_code|SW23|?|?|?|FEDWM706|FEDW255|MF-Extract/ftp|?|Refresh|M|1st00:15|acqxmain|SW23_20140401_000515|fedex5|184360|2014-04-0105:32:07


Last edited by Don Cragun; 04-18-2014 at 05:39 PM.. Reason: Add CODE tags.
# 2  
Old 04-18-2014
Assuming that all of the records for a given table are on adjacent lines in your input file (as in your sample input), the following seems to do what you want:
Code:
awk -F"|" '
function prtable() {
	if(table_name != "") print latest
	latest = $0
	table_name = $2
	time_stamp = $NF
}
$2 != table_name {
	prtable()
	next
}
$NF > time_stamp {
	latest = $0
	time_stamp = $NF
}
END {	prtable()
}' input

If you want to use this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /usr/bin/awk.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding latest file in dir but getting syntax errors

I believe there are couple of syntax issues in my script, couldn't find them :( can someone help me with fixing it to make it work. cd /abcde/ #get the latest filename excluding subdirs filename=`ls -ltr | grep ^- | tail -1 | awk '{print $8}'` #get system date and file timestamp and... (3 Replies)
Discussion started by: simpltyansh
3 Replies

2. UNIX for Dummies Questions & Answers

Display latest record from file based on multiple columns combination

I have requirement to print latest record from file based on multiple columns combination. EWAPE EW1SLE0000 EW1SOMU01 ABORTED 03/16/2015 100004 03/16/2015 100005 001 EWAPE EW1SLE0000 EW1SOMU01 ABORTED 03/18/2015 140003 03/18/2015 140004 001 EWAPE EW1SLE0000 EW1SOMU01 ABORTED 03/18/2015 220006... (1 Reply)
Discussion started by: tmalik79
1 Replies

3. Shell Programming and Scripting

Need help with finding the latest files

Hi, Actually i got a client requirment and i need experts help here. we have 30 parent directories and in that we have so many subdirectories and files. i want to find only latest timestamp files with out touching subdirectories and need to redirect the latest files into some other... (3 Replies)
Discussion started by: lkeswar
3 Replies

4. Shell Programming and Scripting

Help with finding the latest modified version of a file within directories

I am trying to look into multiple directories and pluck out the latest version of a specific file, regardless of where it sits within the directory structure. Ex: The file is a .xls file and could have a depth within the directory of anywhere from 1-5 Working directory - Folder1... (6 Replies)
Discussion started by: co21ss
6 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Finding the latest file in a directory

Hi All, I am using the below command to find the latest file in a dir: ls -tr $v_sftphomedir/$v_sourcefile |tail -1 or ls -t1 $v_sftphomedir/$v_sourcefile |head -1 and the outpur returned is below: /home/cobr_sftp/var/controllingload/Backup/Dbrwds_Div_1796050246.txt I need only the... (5 Replies)
Discussion started by: abhi_123
5 Replies

6. UNIX for Dummies Questions & Answers

Finding latest dir based on it's name (yyyymmdd)

Hi Folks, As part of my application I need to find out what the latest directory based on the name of that directory (not it's file system timestamp). Example: I have a directory which contains below directories (each of while contains files etc) 20120000/ 20120000/ latest (symbolic link to... (5 Replies)
Discussion started by: DOWD_R
5 Replies

7. Shell Programming and Scripting

Finding the Latest file in a Dir

Hi Everyone, I am writing a shell script and I am struck here: I need to find the latest file in a directory depending upon the date. For example: The files in the directory are: Filename_bo_20110619 Filename_bo_20110620 Filename_bo_20110621 Filename_bo_20110622 So here, I want... (2 Replies)
Discussion started by: filter
2 Replies

8. Shell Programming and Scripting

Help in writing a KSH script to filter the latest record?

Hi All, I have a text file with the folowing content. BANGALORE|1417|2010-02-04 08:41:04.174|dob|xxx BANGALORE|1416|2010-02-04 08:23:19.566|dob|yyy BANGALORE|1415|2010-02-04 08:20:14.497|dob|aaa BANGALORE|1414|2010-02-04 08:19:40.065|dob|vvv BANGALORE|1413|2010-02-04... (4 Replies)
Discussion started by: Karpak
4 Replies

9. Shell Programming and Scripting

finding latest file having timestamp on it.....

Hi guys, I have a directory in UNIX having files with the below format, i need to pickup the latest file having recent timestamp embedded on it, then need to rename it to a standard file name. Below is the file format: filename_yyyymmdd.csv, i need to pick the latest and move it with the... (1 Reply)
Discussion started by: kaushik25
1 Replies

10. UNIX for Dummies Questions & Answers

finding latest file in Unix

Hi, i want to search a file in the dir , if file exists for todays date print the message that file found or if file does not exist for todays date/ if file not found i want to display message saying that file not found. How to do this. Thx for your help. (2 Replies)
Discussion started by: nick12
2 Replies
Login or Register to Ask a Question