Compare Timestamps


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare Timestamps
# 1  
Old 08-17-2010
Compare Timestamps

Hi! Long time reader first time registered user and poster.

I've picked up some times and tricks and I'm at a dead end... I've parsed a log file for duplicates and printed only the two fields I need (duplicate entry and time stamp).

My question is, with this output, how would I script something that will compare every two lines and tell me if the timestamp is within 5 minutes of each other:

Code:
for i in `cat numberstest` ; do less test2.txt | grep $i | awk -F " " '{print $3 " " $13}' ; done
05:11:13 ani='13479981111',
05:12:57 ani='13479981111',
16:01:55 ani='14013472222',
16:03:41 ani='14013472222',
14:02:45 ani='19095753333',
15:07:51 ani='19095753333',

Thanks for looking! Smilie

Last edited by Scott; 08-17-2010 at 03:38 PM.. Reason: Added code tags
# 2  
Old 08-17-2010
Quote:
Originally Posted by rexpokinghorn
...
My question is, with this output, how would I script something that will compare every two lines and tell me if the timestamp is within 5 minutes of each other:
...
Code:
$
$
$ cat f5
05:11:13 ani='13479981111',
05:12:57 ani='13479981111',
16:01:55 ani='14013472222',
16:03:41 ani='14013472222',
14:02:45 ani='19095753333',
15:07:51 ani='19095753333',
$
$
$
$ perl -M"Time::Local 'timelocal_nocheck'" -ne 'chomp;
  /^(\d+):(\d+):(\d+) .*$/ and $ts = timelocal_nocheck $3,$2,$1,1,0,2010;
  print $_;
  if ($. > 1) {
    $diff = $ts-$prev;
    $msg = abs($diff)>300 ? "NOT" : "";
    printf("\tDiff = %6d seconds, which is %3s within 5 minutes of previous record.",$diff,$msg);
  }
  print "\n";
  $prev = $ts;
' f5
05:11:13 ani='13479981111',
05:12:57 ani='13479981111',     Diff =    104 seconds, which is     within 5 minutes of previous record.
16:01:55 ani='14013472222',     Diff =  38938 seconds, which is NOT within 5 minutes of previous record.
16:03:41 ani='14013472222',     Diff =    106 seconds, which is     within 5 minutes of previous record.
14:02:45 ani='19095753333',     Diff =  -7256 seconds, which is NOT within 5 minutes of previous record.
15:07:51 ani='19095753333',     Diff =   3906 seconds, which is NOT within 5 minutes of previous record.
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 08-17-2010
Code:
awk '
function TimeToSecond (str) 
{
  split (str,a,":")
  return a[1]*3600+a[2]*60+a[3]
}
function abs(number)
{
  return (number>0)?number:-number
}
NR==1 { start=TimeToSecond($1);print;next}
{ end=TimeToSecond($1)
  diff=end-start
  print $0,diff, "seconds,\t", (abs(diff)>300)?"NOT":X,"within 5 minutes of previous record"
  start=end
}' f5

05:11:13 ani='13479981111',
05:12:57 ani='13479981111', 104 seconds,          within 5 minutes of previous record
16:01:55 ani='14013472222', 38938 seconds,       NOT  within 5 minutes of previous record
16:03:41 ani='14013472222', 106 seconds,          within 5 minutes of previous record
14:02:45 ani='19095753333', -7256 seconds,       NOT  within 5 minutes of previous record
15:07:51 ani='19095753333', 3906 seconds,        NOT  within 5 minutes of previous record

This User Gave Thanks to rdcwayx 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

Write with a look for timestamps

hello i'm using SOX to generate a spectrogram from a wave file with the command : #sox file.wav -n spectrogram is there a way to create a spectrogram using the same command but reading file timestamps instead of the namefile.wav , since name is changing every 4 hours? (it's saved with... (2 Replies)
Discussion started by: Board27
2 Replies

2. Shell Programming and Scripting

Compare two timestamps and print elapsed time

Hi, I am unable to Difference between two time stamps in Linux and display the total elapsed time . Source date: Aug 15, 2012 02:00:03 Target date: Aug 14, 2012 18:00:03 # based on the forums I am using the below function. Converted dates into this format Src_dt=20120814180003... (7 Replies)
Discussion started by: onesuri
7 Replies

3. Shell Programming and Scripting

Comparing two timestamps

Hi all!!, I'm using Ksh and working on Linux. I want to compare two timestamps, timestamp1 and timestamp2. Until, timestamp1 is lesser than timestamp2, i want to do something, lets say print something. The code i have written is: a=`date +%H:%M:%S` b=`date +%H:%M:%S -d" 1... (1 Reply)
Discussion started by: Jayaraman
1 Replies

4. Shell Programming and Scripting

Help with touch and timestamps

Hello fellow Unix geeks, I have been given a very urgent assignment in my office on writing a particular Shell script but I'm very much new to it.I would appreciate any help from you on solving this problem--which might seem very trivial to you. The Unix flavour is a Sun Solaris one..(not... (6 Replies)
Discussion started by: Digjoy83
6 Replies

5. UNIX for Dummies Questions & Answers

Compare 2 timestamps

Hi, i have current timestamp, lets say "12:02:45" in an variable (var1) and another timestamp "08:30:00" fetched from table in another variable2 (var2). How do i compare 2 timestamps in unix shell scripting. if var 1 > var 2 then echo message. Thanks in advance. (3 Replies)
Discussion started by: prasannarajesh
3 Replies

6. Shell Programming and Scripting

perl - Error trying to compare timestamps

Hello, I have the following snippet of code: my $Temp=""; 239 #Fix Timestamp 240 #-------------------------------------------------------------------------------- 241 @initial_date=split(/ /, $field); 242 ... (1 Reply)
Discussion started by: chriss_58
1 Replies

7. Shell Programming and Scripting

Arithmetic on timestamps

Hi Friends, please advise on shell script to add two time stamps for example : a=12:32 b=12:00 c=a+b=00:32 please help me to find shell script to add to two time stamps, as i need to convert time from EST to GMT or SST to prepare status of jobs in unix and to specify estimated time to... (3 Replies)
Discussion started by: balireddy_77
3 Replies

8. Shell Programming and Scripting

timestamps

Hello! I have the following problem. I read a file using perl, each line of this file has the fllowing format. 14/4/2008 8:42:03 πμ|10800|306973223399|4917622951117|1||1259|1|126|492|433||19774859454$ Th first field is the timestamp and the second field is the offset in seconds. How can... (1 Reply)
Discussion started by: chriss_58
1 Replies

9. Solaris

Difference between two timestamps

I'm writting a script to find the difference between two timestamp. One field i get on delivery time of the file like 07:17 AM and other is my SLA time 06:30 AM I need to find the difference between these two time (time exceeded to meet SLA). Need some suggestions. (8 Replies)
Discussion started by: raman1605
8 Replies

10. UNIX for Dummies Questions & Answers

Unix timestamps

Can someone help me with a Unix or perl script to convert the unix timestamps to human readable format? Any help will be highly appreciated... (3 Replies)
Discussion started by: hamsasal
3 Replies
Login or Register to Ask a Question