Date Range Problem

Hi All,

I have a log file which has first few characters of every line as a timestamp.

2010-06-01 04:56:02,802 DEBUG {Thread-27}  Some text message
2010-06-01 04:56:02,802 DEBUG {Thread-27}  Some text message
2010-06-01 04:56:02,802 DEBUG {Thread-27}  Some text message
2010-06-01 04:56:02,802 DEBUG {Thread-27}  Some text message
2010-06-01 05:22:02,802 DEBUG {Thread-27}  Some text message
2010-06-01 05:22:02,802 DEBUG {Thread-27}  Some text message
2010-06-01 05:22:02,802 DEBUG {Thread-27}  Some text message
2010-06-01 05:22:02,802 DEBUG {Thread-27}  Some text message
2010-06-01 06:43:02,802 INFO  {Thread-27}  Some text message
2010-06-01 06:43:02,803 INFO  {Thread-27}  Some text message
2010-06-01 06:43:02,804 INFO  {Thread-27}  Some text message
2010-06-01 06:43:02,804 INFO  {Thread-27}  Some text message
2010-06-01 06:43:02,809 DEBUG {Thread-27}  Some text message
2010-06-01 06:43:02,809 DEBUG {Thread-27}  Some text message
2010-06-01 06:43:02,809 DEBUG {Thread-27}  Some text message
2010-06-01 07:08:02,809 DEBUG {Thread-27}  Some text message
2010-06-01 07:08:02,809 DEBUG {Thread-27}  Some text message

My aim to find all such lines which have the timestamp of 1 hr before the current time.

How can this be achieved?

TIA,
Nitin.

is your date is GNU date ?

provide the output of

uname -a
Nitin--Bld-17:~ nitin$ uname -a
Darwin Nitin--Bld-17.local 9.7.0 Darwin Kernel Version 9.7.0: Tue Mar 31 22:52:17 PDT 2009; root:xnu-1228.12.14~1/RELEASE_I386 i386
Nitin--Bld-17:~ nitin$ 

check whether this works for you

last_time=`tail -1 date_file | awk '{print $2}'`
one_hour_ago=`date -d "$last_time 1 hour ago" "+%Y-%m-%d %H:[0-5][0-9]"`

echo $last_time
echo $one_hour_ago

first_cut_point=`grep -n -m 1 "$one_hour_ago" date_file | awk -F":" '{print $1}'`
second_cut_point=`grep -n -m 1 "$last_time" date_file | awk -F":" '{print $1}'`

echo $first_cut_point
echo  $second_cut_point
sed -n ${first_cut_point},${second_cut_point}p date_file

Getting an error. Dont know why :frowning:

Nitin--Bld-17:UnixScriptTest nitin$ last_time=`tail -1 times.txt | awk '{print $2}'`
Nitin--Bld-17:UnixScriptTest nitin$ echo $last_time
12:31:34.0707
Nitin--Bld-17:UnixScriptTest nitin$ one_hour_ago=`date -d "$last_time 1 hour ago" "+%Y-%m-%d %H:[0-5][0-9]"`
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
Nitin--Bld-17:UnixScriptTest nitin$ 

try to execute the below command..

date -d "1 hour ago"

if the above command is not executing successfully, then my script will not work. then you have to write a seperate logic to find out the one hour less time

1 Like

if you don't have GNU date, perhaps the below thread could help you.

date arithmetic

Also,
Various threads and FAQs about date arithmetic

1 Like

Thank you itkamaraj. Your idea of sed and grep with line numbers worked for me.
Tweaked it a bit. And it worked.

Thanks again.