LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2010, 01:35 AM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Rep: Reputation: 15
Removing duplicates with offset (datetime)


I have a column of datetime entries which I sorted to removed duplicate entries. I have still lots of entries which are adjacent to each other by 1 second. How would I go about removing any entries which have an offset of the previous or after entry by 1 second?

Code:
2010-10-19 00:00:59;
2010-10-19 00:04:38; <--
2010-10-19 00:04:39; <--
2010-10-19 00:05:27; 
2010-10-19 00:10:14; <--
2010-10-19 00:10:15; <--
2010-10-19 00:10:31;
2010-10-19 00:12:14;
2010-10-19 00:14:04; <--
2010-10-19 00:14:05; <--
2010-10-19 00:14:06; <--
2010-10-19 00:15:19;
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 10-20-2010, 05:11 AM   #2
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
Wink

Convert the date-time into seconds since 1970. You then have a list of numbers, e.g.
List A:
Code:
10 19 20 30 31 32 40
Add 1 to the list:
List B:
Code:
11 20 21 31 32 33 41
For each 'b' in List B,
If 'b' is found in List A,
Then remove 'b' and 'b'-1 in List A.

The remainders in List A is the timestamps you need.

Last edited by Jerry Mcguire; 10-20-2010 at 05:35 AM. Reason: made a mistake
 
Old 10-20-2010, 05:39 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A solution in GNU awk:
Code:
{
  datespec = gensub(/[-:;]/," ","g")
  if ( mktime(datespec) - pre > 1 ) print
  pre = mktime(datespec)
}
 
2 members found this post helpful.
Old 10-20-2010, 05:49 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Wasn't sure from the description if you want to print the first one found or the last. This does the last:
Code:
awk -F"[:;]" 'NR > 1 && (sec > $(NF-1)+1 || sec < $(NF-1)-1){print line;last=sec}{line=$0;sec=$(NF-1)}END{if(sec > last + 1 || sec < last -1)print}' file
 
1 members found this post helpful.
Old 10-20-2010, 05:58 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well that looks a lot better ... I always forget about the time functions
 
Old 10-20-2010, 07:28 PM   #6
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
Thank you colucix and grail. Much appreciated.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging files and removing near-duplicates TheBigH Linux - Newbie 3 12-02-2009 04:24 PM
[SOLVED] datetime values offset when restoring from db dump? sneakyimp Linux - Software 2 10-05-2009 01:59 PM
LXer: Sorting Perl Lists And Removing Duplicates On Linux Or Unix LXer Syndicated Linux News 0 09-04-2008 05:20 AM
Perl DateTime abdul_zu Linux - General 1 01-14-2006 02:55 AM
php :: datetime gmarais Programming 3 03-06-2004 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration