To check time stamp in log file and calculate.

Hi Friends,

I have the following logfile.
i want to make a script for calculate time by time2 - time1

1600266278|random|1|2014-09-19 02:08:56.024|2014-09-19 02:08:59.398|A|B|ROOM|Num0208559970111101788|1|dog|dos
1600266200|random|4|2014-09-19 02:08:06.572|2014-09-19 02:08:44.728|A|B|ROOM|Num02080653902165193|1|dog|cat
1600266233|random|4|2014-09-19 02:08:18.920|2014-09-19 02:08:19.067|A|B|ROOM|Num0208188890111101772|2|dog|cat
1600266241|random|4|2014-09-19 02:08:25.638|2014-09-19 02:08:25.778|A|B|ROOM|Num0208256030111101773|1|dog|cat
1600266249|random|4|2014-09-19 02:08:28.291|2014-09-19 02:08:55.459|A|B|ROOM|Num02082824802165203|5|dog|cat
1600266285|random|4|2014-09-19 02:08:39.946|2014-09-19 02:08:40.059|A|B|ROOM|Num02083991402165208|4|dog|cat
1600266303|random|4|2014-09-19 02:08:46.010|2014-09-19 02:08:46.157|A|B|ROOM|Num02084596502165210|1|dtac|cat
1600266221|random|1|2014-09-19 02:08:47.239|2014-09-19 02:08:47.372|A|B|ROOM|Num0208472100111101784|2|dtac|dos
1600266283|random|1|2014-09-19 02:08:48.904|2014-09-19 02:09:49.031|A|B|ROOM|Num02084886002165211|7|dog|dos

please help me!:frowning:

If you have GNU date, you could convert them easily to "epoch" times, and do arithmetic subtraction.

i.e.:

$ date -d "2014-09-19 02:09:56.024" "+%s"
1411085396

Or maybe there's something in Perderabo's Datecalc script that can help you.

Give this perl script a try...

#!/usr/bin/perl

use Time::Local;

while (<>) {
    @f = split /\|/, $_;

    ($y1, $o1, $d1) = (substr($f[3], 0, 4), substr($f[3], 5, 2), substr($f[3], 8, 2));
    ($h1, $m1, $s1) = (substr($f[3], 11, 2), substr($f[3], 14, 2), substr($f[3], 17, 2));
    $ms1 = substr($f[3], 20, 3);

    ($y2, $o2, $d2) = (substr($f[4], 0, 4), substr($f[4], 5, 2), substr($f[4], 8, 2));
    ($h2, $m2, $s2) = (substr($f[4], 11, 2), substr($f[4], 14, 2), substr($f[4], 17, 2));
    $ms2 = substr($f[4], 20, 3);

    $t2 = timelocal($s2, $m2, $h2, $d2, $o2-1, $y2-1900);
    $t1 = timelocal($s1, $m1, $h1, $d1, $o1-1, $y1-1900);
   
    printf("t2=%d.%03d\tt1=%d.%03d\tt2-t1=%g\n", $t2, $ms2, $t1, $ms1, ($t2-$t1) + (($ms2-$ms1)/1000));
}
1 Like