How to Compare timestamp | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to Compare timestamp

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2012
vsachan vsachan is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 26 October 2013, 12:11 AM EDT
Posts: 39
Thanks: 9
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 02-07-2012
clx clx is offline Forum Advisor  
Registered User
 
Join Date: Jun 2007
Last Activity: 31 October 2014, 8:56 AM EDT
Location: Mumbai, India
Posts: 1,614
Thanks: 125
Thanked 184 Times in 178 Posts
Your requirement doesn't see to be real.
what about the date?
Does your database/file contain the data only for the sysdate?
Sponsored Links
    #3  
Old 02-07-2012
Shirishlnx Shirishlnx is offline
Registered User
 
Join Date: Feb 2012
Last Activity: 20 May 2014, 3:00 AM EDT
Location: India
Posts: 112
Thanks: 9
Thanked 3 Times in 3 Posts
U can try by converting time H:M to Seconds and compare that ...
    #4  
Old 02-07-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 November 2014, 10:03 AM EST
Location: Saskatchewan
Posts: 19,905
Thanks: 845
Thanked 3,415 Times in 3,200 Posts
How about an alphabetic comparison?
Code:
awk -F, -v T1="20:06:00" -v T2="20:09:00" '(T1>=$2)&&(T2<=$2)'

Sponsored Links
    #5  
Old 02-07-2012
vsachan vsachan is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 26 October 2013, 12:11 AM EDT
Posts: 39
Thanks: 9
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old 02-07-2012
parthmittal2007 parthmittal2007 is offline
Banned
 
Join Date: Dec 2011
Last Activity: 5 March 2013, 1:31 AM EST
Posts: 193
Thanks: 22
Thanked 2 Times in 2 Posts

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
Sponsored Links
    #7  
Old 02-07-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 November 2014, 10:03 AM EST
Location: Saskatchewan
Posts: 19,905
Thanks: 845
Thanked 3,415 Times in 3,200 Posts
Quote:
Originally Posted by vsachan View Post
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 ().
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to compare a file by its timestamp and store in a different location whenever timestamp changes? mailsara UNIX for Dummies Questions & Answers 2 07-25-2011 04:48 PM
Compare current time to timestamp on a file cillmor Shell Programming and Scripting 1 04-01-2010 09:32 AM
how to grep and compare timestamp in a file with the current date achu AIX 1 05-25-2009 10:05 AM
how to grep and compare timestamp in a file with the current date achu AIX 1 05-25-2009 09:20 AM
how to compare the timestamp of 2 files mail2sant Shell Programming and Scripting 2 04-07-2008 05:43 AM



All times are GMT -4. The time now is 07:39 PM.