Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)
# 1  
Old 11-22-2013
Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile:

Userids Date Time
acb Checkout time: 2013-11-20 17:00
axy Checkout time: 2013-11-22 12:00
der Checkout time: 2013-11-17 17:00
xyz Checkout time: 2013-11-19 16:00
ddd Checkout time: 2013-11-21 16:00
aaa Checkout time: 2013-11-18 17:00
yyy Checkout time: 2013-11-22 12:00


Objective: I like to sort out userid who have checked out the application for more than 24hours.

Let's assume I have cronjob scheduled to run at 17:00 on Nov 22nd 2013, If Checkout time > 24hrs then, expected results should look like below:

acb User Checked out license for more than 2 days
der User Checked out license for more than 5 days
xyz User Checked out license for more than 2 days
ddd User Checked out license for more than 1 days
aaa User Checked out license for more than 4 days


I was trying to covert $4 & $5 to epoch seconds using (date +%s -d "$3 $4") and subtract this from (date +%s) if difference is > 24hrs (86400Sec) then print user id. But lacking my knowledge to get variable right.

Thanks for your help
# 2  
Old 11-22-2013
What are $4 and $5? That looks like awk, not shell. It is useful to know what language you are expecting us to program in Smilie
# 3  
Old 11-22-2013
Yes, I am looking to acheive this in "awk"
# 4  
Old 11-22-2013
You can feed those strings into GNU awk's or mawk's mktime function:

Code:
sub(/:/, " ", $4); TS=mktime($3 " " $4 " 00" );

# 5  
Old 11-22-2013
I executed following awk command to print Variable “TS”
awk ' {sub(/:/, " ", $5); TS=mktime($4 " " $5 " 00" ); print TS}' logfile

Output:
1323644400
1323453600
1323903600
1323727200
1323554400
1323817200
1323453600
-1

I have question these seconds are not match if I output epoch second using date +%s -d "2013-11-20 17:00 " it's giving me 1384988400 seconds which not matching with 1323644400 1st lineof output.
And Why there -1 at the end of program.

Is there way to use date i.e date +%s -d "2013-11-20 17:00 .

Please suggest I am not routine Linux user

Thanks!
# 6  
Old 11-22-2013
Huh, that is a large difference. Check what the $4 " " $5 " 00" actually becomes? awk's mktime() expects "YYYY MM DD HH MM SS"

[edit] Add a sub(/-/, " ", $4); statement to the program, the dashes are probably confusing it.

Running GNU date 9,000 times is not going to be very efficient at all. if you have mktime() in awk it's best to use it.

Last edited by Corona688; 11-22-2013 at 03:51 PM..
# 7  
Old 11-22-2013
Removed “-“ from date format
excecuted cmd: awk ' {sub(/:/, " ", $5);sub(/-/, " ", $4); TS=mktime($4 " " $5 " 00" ); print TS}' timestamp.txt

Output:
1381528800
1381338000
1381788000
1381611600
1381438800
1381701600
1381338000
-1

Still the difference is very large.

I am not expecting logfile more than 150lines (50 to 150 max), That's why I was looking into GNU date which is easy for epoch conversion.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert a future date into epoch seconds on HPUX system

Hi All, I have scenario where i have to compare two dates. I thought of converting them to epoch seconds and do a numeric comparison. This works fine on Linux systems. $ date -d '2015/12/31' +%s 1451538000 $ date +%s 1449159121 But we don't have -d option in HPUX. What would be... (5 Replies)
Discussion started by: veeresh_15
5 Replies

2. Shell Programming and Scripting

Awk: time intervals based on epoch time

I have a list of epoch times delimited by "-" as follows: 1335078000 - 1335176700 1335340800 - 1335527400 1335771300 - 1335945600 1336201200 - 1336218000 The corresponding dates are: 20120422 1000 - 20120423 1325 20120425 1100 - 20120427 1450 20120430 1035 - 20120502 1100 ... (3 Replies)
Discussion started by: alex2005
3 Replies

3. Shell Programming and Scripting

Find time difference based on logfile

Hi All, Firstly thank you for the forum members I need to find time difference b'w two rows of timestamp using awk/shell. Here is the logfile: cat business_file start:skdjh:22:06:2010:10:30:22 sdfnskjoeirg wregn'wergnoeirnfqoeitgherg end:siifneworigo:22:06:2010:10:45:34... (3 Replies)
Discussion started by: Srinivas Gadi
3 Replies

4. Shell Programming and Scripting

Current triggered time to epoch seconds

I have a requirement to find long running instances for notifying the stake holders based on the triggered time in AIX. I am not sure how to convert the triggered time to epoch seconds. For example : Current triggered time of instance is 13:06:19 -> how to convert this into epoch in the... (5 Replies)
Discussion started by: chandu123
5 Replies

5. Shell Programming and Scripting

Adding Seconds to UNIX/Epoch-Time

Hello All, I have a Perl script I'm writing where I ask the user to enter a "start time" for something. The "$start_time" will be in the format of: # The Time CLI Option Can be in the format of: --start-time="1day" --start-time="2hours" --start-time="45min" ... (1 Reply)
Discussion started by: mrm5102
1 Replies

6. Shell Programming and Scripting

Based on the first & last timestamp of the file, need to calculate the time taken to complete

Below is the sample file: 287 DEBUG syndesis.pb.util.ITraceManager - syOID=ELntNetwork:1005Mon Oct 15 17:18:21 IST 2012 <ELClientManagerenEmsSession() > Setting Java Properties 287 DEBUG syndesis.pb.util.ITraceManager - syOID=ELntNetwork:1005Mon Oct 15 17:18:21 IST 2012... (1 Reply)
Discussion started by: ashok.kumar
1 Replies

7. Shell Programming and Scripting

Transpose timestamp based on column values and calculate time difference

Hello Expert, I need to transpose Date-Timestamp based on same column values and calculate time difference. The input file would be as below and required output is mentioned in the bottom INPUT File ======== 08/23/2012 12:36:09 JOB_5340 08/23/2012 12:36:14 JOB_5340 08/23/2012... (2 Replies)
Discussion started by: asnandhakumar
2 Replies

8. Shell Programming and Scripting

Date conversion from Standard/given format to seconds/epoch

I am trying get time difference of two dates in secs. Initially I want to convert a standard date format to epoch for two dates and then subtract the two epoch dates. Example : date -d "2007-09-01 17:30:40" '+%s' But this gives me below error date: illegal option -- d Usage: date OS: AIX... (6 Replies)
Discussion started by: bpaac
6 Replies

9. Programming

How to search a file based on a time stamp backwards 10 seconds

Hi all, I'm after some help with this small issue which i'm struggling to work out a fix for. I have a file that contains records that all have a time stamp for each individual record, i need to search the file for a specific time stamp and then search back 10 seconds to see if the number... (2 Replies)
Discussion started by: sp3arsy
2 Replies

10. Shell Programming and Scripting

conversion from EPOCH timestamp to local time zone

hello gurus, i want a perl/shell script which once invoked should convert a set of EPOCH timestamps to local time ( IST..i want) . how does it work ,i have an idea on that..but writing a perl/shell script for it is not possible for me...so i need help for the same. my exact requirement is... (2 Replies)
Discussion started by: abhijeetkul
2 Replies
Login or Register to Ask a Question