Need help in a time-grep Script

i have a script that will do the following:

1) find the 5 min before time from the current time
2) Grep the data from a file from current time to the 5 min before time

below the code:

#!/bin/ksh
current_time=`date +%s`
echo "$current_time"

perl -le 'print scalar localtime('$current_time');' | awk '{print $4}' | cut -c 1-5
a1=`perl -le 'print scalar localtime('$current_time');' | awk '{print $4}' | cut -c 1`
a2=`perl -le 'print scalar localtime('$current_time');' | awk '{print $4}' | cut -c 2`
a4=`perl -le 'print scalar localtime('$current_time');' | awk '{print $4}' | cut -c 4`
a5=`perl -le 'print scalar localtime('$current_time');' | awk '{print $4}' | cut -c 5`

echo "$a1" "$a2" "$a4" "$a5"

echo "Enter the seconds to be added"
read seconds1
echo "$seconds1"

sum=$(( $current_time - $seconds1 ))
echo "$sum"

perl -le 'print scalar localtime('$sum');' | awk '{print $4}' | cut -c 1-5
b1=`perl -le 'print scalar localtime('$sum');' | awk '{print $4}' | cut -c 1`
b2=`perl -le 'print scalar localtime('$sum');' | awk '{print $4}' | cut -c 2`
b4=`perl -le 'print scalar localtime('$sum');' | awk '{print $4}' | cut -c 4`
b5=`perl -le 'print scalar localtime('$sum');' | awk '{print $4}' | cut -c 5`

echo "$b1" "$b2" "$b4" "$b5"

output


1235383292
04:01
0 4 0 1
Enter the seconds to be added
300
300
1235382992
03:56
0 3 5 6

The problem here i m facing is how to grep the data from the file from time 03:56 to 04:01 ??

Below the file content:

Feb 20, 2009 12:59:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO: ---------------------------------------------------------------
Feb 20, 2009 03:56:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO: ---------------------------------------------------------------
Feb 20, 2009 03:57:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:           authType=null
Feb 20, 2009 03:59:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:      contentLength=-1
Feb 20, 2009 03:59:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:        contentType=text/xml;charset=utf-8
Feb 20, 2009 04:00:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:             header=Content-Type=text/xml;charset=utf-8
Feb 20, 2009 04:01:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:             header=Transfer-Encoding=chunked
Feb 20, 2009 04:02:51 AM org.apache.catalina.valves.RequestDumperValve invoke

I suggest you download and install the logwatch utilities and also the Perl Date::Manip module from CPAN. Combined, you can enter a date or time range and get back a regular expression which will match exactly and only that time range. You can do it by hand, too, with the perl re like this:

$_ =~ m/^(Feb 20, 2009 03:5[6-9]:..|Feb 20, 2009 04:0[01]:..)/;

If you want to get until 4:12, you'll need a third sub-expression:

$_ =~ m/^(Feb 20, 2009 03:5[6-9]:..|Feb 20, 2009 04:0[0-9]:..|Feb 20, 2009 04:1[012]:..)/;

Do you see the pattern now?

i m using the below logic using the awk command: but not getting the correct o/p when running the script:


#!/bin/ksh
current_time=`date +%s`
echo "$current_time"

perl -le 'print scalar localtime('$current_time');' | awk '{print $4}' | cut -c 1-5
echo "Enter the seconds to be subtracted"
read seconds1
echo "$seconds1"

sum=$(( $current_time - $seconds1 ))
echo "$sum"

perl -le 'print scalar localtime('$sum');' | awk '{print $4}' | cut -c 1-5
b1=`perl -le 'print scalar localtime('$sum');' | awk '{print $4}' | cut -c 1`
b2=`perl -le 'print scalar localtime('$sum');' | awk '{print $4}' | cut -c 2`

echo "$b1" "$b2" 
cd /ednadtu3/home/webapp/apache-tomcat-5.5.15/logs
awk '/[$b1-9][$b2-9]:[0-9][0-9]:/{c=2}c&&c--' sss.txt

output i m getting:

1235390785
06:06

Enter the seconds to be subtracted
7600
7600
1235383185
03:59
0 3 

Feb 20, 2009 12:59:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO: ---------------------------------------------------------------

What i want:

Feb 20, 2009 03:56:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO: ---------------------------------------------------------------
Feb 20, 2009 03:57:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:           authType=null
Feb 20, 2009 03:59:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:      contentLength=-1
Feb 20, 2009 03:59:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:        contentType=text/xml;charset=utf-8
Feb 20, 2009 04:00:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:             header=Content-Type=text/xml;charset=utf-8
Feb 20, 2009 04:01:51 AM org.apache.catalina.valves.RequestDumperValve invoke
INFO:             header=Transfer-Encoding=chunked
Feb 20, 2009 04:02:51 AM org.apache.catalina.valves.RequestDumperValve invoke

Please spell out "output". OP often refers to "original poster" and other things. Output is just 3 characters longer than "o/p". Please pass this along to all your colleagues. Thank you.

Anyway, in answer to your question, it's not working with awk because you are using single-quotes around the regular expression, and this prevents "$b" from being expanded into what you want. Use double-quotes in this case (though using double quotes in general for awk scripts is a bad idea.)