How to Compare timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Compare timestamp
# 1  
Old 02-07-2012
Question How to Compare timestamp

Hi,
my script is accepting the time from user. Then I have to select all records from file that lie between the time stamp supplied by user.

For eg
File format:
-----------
Code:
25-01-2012,20:06:27, ,09454545455,099999999999999,105,105, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,
25-01-2012,20:06:39, ,09454545455,099999999999999,994,994, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,
25-01-2012,20:06:39, ,094545455,099999999999999,88,88, , ,TTK32,9999, ,MUO2, ,O, ,99, , ,
25-01-2012,20:06:42, ,094545455,099999999999999,96,96, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,
25-01-2012,20:06:43, ,094545455,0999999999999,111,111, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,
25-01-2012,20:06:43, ,09455455,099999999999999,115,115, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,
25-01-2012,20:06:44, ,09454545455,099999999999999,83,83, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,
25-01-2012,20:06:51, ,09454545455,099999999999,114,114, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,
25-01-2012,20:06:52, ,094545455,099999999999999,161,161, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,
25-01-2012,20:06:52, ,09454545455,099999999999,96,96, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,
25-01-2012,20:06:53, ,09454545455,099999999999999,91,91, , ,TTK32,9999, ,PPUU, ,O, ,99, , ,


if The user is supplying
Code:
Time1=20:06:00
Time1=20:09:00

then we have to select all records whose time stamp is between these 2 times

Please suggest.

Last edited by Franklin52; 02-08-2012 at 06:10 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-07-2012
Your requirement doesn't see to be real.
what about the date?
Does your database/file contain the data only for the sysdate?
# 3  
Old 02-07-2012
U can try by converting time H:M to Seconds and compare that ...
# 4  
Old 02-07-2012
How about an alphabetic comparison?
Code:
awk -F, -v T1="20:06:00" -v T2="20:09:00" '(T1>=$2)&&(T2<=$2)'

# 5  
Old 02-07-2012
Thanks Corona...
But it is giving error...
Code:
gunzip -c ABCDEF.gz | awk -F, -v T1="20:00:00" -v T2="20:01:00" '{if(T1>=$2)&&(T2<=$2) print $0'

error is:
Code:
 Syntax Error The source line is 1.
 The error context is
                 >>> {if(T1>=$2)&& <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.


Last edited by Franklin52; 02-08-2012 at 06:15 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 02-07-2012
Code:
c1=`echo $1 | awk -F":" '{print $1$2$3}'`
c2=`echo $2 | awk -F":" '{print $1$2$3}'`

cat file | while read line
do
c=`echo $line | cut -f2 -d"," | awk -F":" '{print $1$2$3}'`

if [ $c -gt $c1 -a  $c -lt $c2 ];then
echo $line
fi
done


Last edited by Franklin52; 02-08-2012 at 06:15 AM.. Reason: Please use code tags for code and data samples, thank you
# 7  
Old 02-07-2012
Quote:
Originally Posted by vsachan
Thanks Corona...
But it is giving error...

gunzip -c ABCDEF.gz | awk -F, -v T1="20:00:00" -v T2="20:01:00" '{if(T1>=$2)&&(T2<=$2) print $0'


error is:
Syntax Error The source line is 1.
The error context is
>>> {if(T1>=$2)&& <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
Try the code I actually gave you instead of rewriting it into something broken.

The version you wrote is very redundant( X { print $0 } is equivalent to just X ) and missing tons of important brackets -- i.e. you forgot the ending }, and didn't put the if-statement in its own ().
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep lines between last hour timestamp and current timestamp

So basically I have a log file and each line in this log file starts with a timestamp: MON DD HH:MM:SS SEP 15 07:30:01 I need to grep all the lines between last hour timestamp and current timestamp. Then these lines will be moved to a tmp file from which I will grep for particular strings. ... (1 Reply)
Discussion started by: nms
1 Replies

2. Shell Programming and Scripting

AIX : Need to convert UNIX Timestamp to normal timestamp

Hello , I am working on AIX. I have to convert Unix timestamp to normal timestamp. Below is the file. The Unix timestamp will always be preceded by EFFECTIVE_TIME as first field as shown and there could be multiple EFFECTIVE_TIME in the file : 3.txt Contents of... (6 Replies)
Discussion started by: rahul2662
6 Replies

3. Shell Programming and Scripting

To check timestamp in logfile and display lines upto 3 hours before current timestamp

Hi Friends, I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only I can get the... (12 Replies)
Discussion started by: srkmish
12 Replies

4. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

5. Shell Programming and Scripting

Getting a relative timestamp from timestamp stored in a file

Hi, I've a file in the following format 1999-APR-8 17:31:06 1500 3 45 1999-APR-8 17:31:15 1500 3 45 1999-APR-8 17:31:25 1500 3 45 1999-APR-8 17:31:30 1500 3 45 1999-APR-8 17:31:55 1500 3 45 1999-APR-8 17:32:06 1500 3 ... (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

6. Shell Programming and Scripting

Compare current time to timestamp on a file

I'm trying to compare 2 dates between current time and the timestamp on a file. The date format is mmdd Both return Apr 1 but when using if statement line 11: Apr 1: command not found error is returned #!/bin/sh log="DateLog" Current_Date=`date +%b%e` Filepmdate=`ls -l /file.txt |... (1 Reply)
Discussion started by: cillmor
1 Replies

7. Shell Programming and Scripting

Compare file timestamp with current date. Diff must be 1 hour.

Hello, I've created the script below to compare the content of two files with a delay of an hour. After an hour, the lines that exist in both files, will be printed and executed. The script now uses a counter to countdown 50 minutes. But what I would prefer is to check the file timestamp of... (3 Replies)
Discussion started by: taipan
3 Replies

8. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the log file each line starts with timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file... (1 Reply)
Discussion started by: achu
1 Replies

9. AIX

how to grep and compare timestamp in a file with the current date

I want to read a log file from a particular location.In the logfile , lines contains timestamp.I need to compare the timestamp in the logfile with the current date.If the timpestamp in the log file is less than 4 hours then i need to read the file from that location.Below is the file format.Please... (1 Reply)
Discussion started by: achu
1 Replies

10. Shell Programming and Scripting

how to compare the timestamp of 2 files

i want to compare the timestamp of the 2 file.. please let me know how we can achive this.. (2 Replies)
Discussion started by: mail2sant
2 Replies
Login or Register to Ask a Question