To check time stamp in log file and calculate.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To check time stamp in log file and calculate.
# 1  
Old 09-18-2014
Computer 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
Code:
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!Smilie
# 2  
Old 09-18-2014
If you have GNU date, you could convert them easily to "epoch" times, and do arithmetic subtraction.

i.e.:
Code:
$ 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.
# 3  
Old 09-19-2014
Give this perl script a try...
Code:
#!/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));
}


Last edited by shamrock; 09-19-2014 at 10:37 PM.. Reason: botched printf
This User Gave Thanks to shamrock 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

Shell Script | Parse log file after a given date and time stamp

I am developing one script which will take log file name, output file name, date, hour and minute as an argument and based on these inputs, the script will scan and capture all the error(s) that have been triggered from a given time. Example: script should capture all the error after 13:50 on Jan... (2 Replies)
Discussion started by: ROMA3
2 Replies

2. Shell Programming and Scripting

Collecting all lines between two time stamp from the log

Can you help me to collect the entire logs between two time stamp. The below awk command collecting the logs only if the line has time stamp. awk '$0>=from && $0<=to' from="150318 23:19:04" to="150318 23:55:04" log file 150318 23:19:04 logentries 150318 23:29:04 logentries 150318... (11 Replies)
Discussion started by: zenkarthi
11 Replies

3. Shell Programming and Scripting

Check time stamp formate in file

I have to check whether last line of the file has time stamp or not. File.txt cong = 0 This formats a date and time into the given strftime() format = 09 Dates can be passed to Smarty as unix timestamps = 56789 mysql timestamps or any string made up =98 Since Smarty-2.6.10 numeric... (4 Replies)
Discussion started by: Roozo
4 Replies

4. Shell Programming and Scripting

awk - check time stamp between range or not

I want to check given time stamp is between the given time stamp or not. I am using AIX. YYYYMMDDHHMMSS abc.csv START TIME, END TIME 20130209018000,20130509022000 20120209018000,20130509022000 20120209018000,20130509022000 Script will check given time stamp is between above two range or... (2 Replies)
Discussion started by: vegasluxor
2 Replies

5. Shell Programming and Scripting

Check file time stamp

Hi, I need help to read file in a directory on basis of time stamp. e.g. If file access in last 2 minutes it should not be copy to remote directory. Below is my script. #!/bin/ksh DATE=`date +"%Y-%m-%d_%H%M"` SEPARATER=" " exec < out_interfaces.cfg while read source_path... (10 Replies)
Discussion started by: qamar.alam
10 Replies

6. Shell Programming and Scripting

Calculate time stamp difference

Hi All, I am new to shell scripting.I have to write a shell script for the problem statement: "A file is updated continously. If it is not updated for a day then an error message needs to pop up." So the script needs to read the last modified time of that file and current system time .If... (4 Replies)
Discussion started by: bharath.phatak
4 Replies

7. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

8. Shell Programming and Scripting

Calculate the Time stamp difference

hi, I have a log file which gives time stamps hh:mm:ss.sssss format in which hh- hours , mm -minutes ss.sssss - seconds.microseconds I need to calculate the time diff between sent time stamp and received time stamp .... could any one please help me.. i am tryin to write a script but... (2 Replies)
Discussion started by: firestar
2 Replies

9. Shell Programming and Scripting

Extract info from log file and compute using time date stamp

Looking for a shell script or a simple perl script . I am new to scripting and not very good at it . I have 2 directories . One of them holds a text file with list of files in it and the second one is a daily log which shows the file completion time. I need to co-relate both and make a report. ... (0 Replies)
Discussion started by: breez_drew
0 Replies

10. UNIX for Dummies Questions & Answers

Inserting Date&Time Stamp In Existing Log File

I am trying to insert a line with a date stamp in a file that is used to monitor activity in one of our directories. By doing this, I want to grep that file each day and go to the last entry for each time a error occurred and pull all errors generated if any exist. If error exists I want that error... (3 Replies)
Discussion started by: shephardfamily
3 Replies
Login or Register to Ask a Question