Need help in a time-grep Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in a time-grep Script
# 1  
Old 02-23-2009
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:
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
Code:
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:
Code:
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


Last edited by ali560045; 02-23-2009 at 06:22 AM..
# 2  
Old 02-23-2009
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:
Code:
$_ =~ 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:
Code:
$_ =~ 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?
# 3  
Old 02-23-2009
i m using the below logic using the awk command: but not getting the correct o/p when running the script:

Code:
#!/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:
Code:
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:
Code:
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


Last edited by ali560045; 02-23-2009 at 08:15 AM..
# 4  
Old 02-23-2009
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.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep with time constraints

Hello Friends - I am trying to grep certain messages that have a time slot like this: MyRate=33FC|SystemDEF=445DE|Calc=33W2|Time=15:50:24 I am trying to grep everything after Time=15:50:26 including SystemDEF=E2S and Calc=33W2 into a file called myrate.dat Not able to... (7 Replies)
Discussion started by: DallasT
7 Replies

2. Shell Programming and Scripting

Time taken to run grep in different OS

Hi , I am greping a keyword in all sql files in Solaris and Linux. Solaris bash-3.00$ time grep -iwc BEN_STARTUP_LERS_TL084701_WHO *.sql Load__v20130719-prod.sql:0 Load__v20130719-prod.sql:0 Load__v20130719-prod.sql:0 Load__v20130719-prod.sql:0 Load__v20130719-prod.sql:0... (4 Replies)
Discussion started by: millan
4 Replies

3. Shell Programming and Scripting

Ftp script hangs for first time,but works every second time

Hi I have an ftp script which works fine when i execute through a test scheduler(UC4), but when i run it through the prod scheduler(UC4), it hungs indefinetely, when we cancel the job and re-run it it works perfectly fine. here is the code,, any idea why this is happening ???? ... (1 Reply)
Discussion started by: selvankj
1 Replies

4. Shell Programming and Scripting

grep the time within given minutes

Mar 26 15:25:11 : jdoe : TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; COMMAND=/usr/bin/su - Mar 26 15:28:52 : jdoe : 3 incorrect password attempts ; TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; COMMAND=/usr/bin/su - Mar 25 12:23:07 : jdoe : TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; ... (6 Replies)
Discussion started by: Daniel Gate
6 Replies

5. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

6. Shell Programming and Scripting

how to grep the time of the mail received?

Hi all, My question is how can we grep the time of the mail which we receive in our inbox? like if i get a mail tonight at 10 i should be able to grep the time of it tomorrow morning or someother day in 24 hr format.... how can we do that? (2 Replies)
Discussion started by: smarty86
2 Replies

7. Homework & Coursework Questions

Grep for modified time

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: is it possible to come up with a list of files that are modified before a certain number of hours only using the... (3 Replies)
Discussion started by: momo.reina
3 Replies

8. Shell Programming and Scripting

Grep for modified time

is it possible to come up with a list of files that are modified before a certain number of hours only using the grep command? ex. list files that were modified less than 10 hours ago i've only managed to list files that were created on the same day, i can't seem to figure out how to work... (3 Replies)
Discussion started by: momo.reina
3 Replies

9. Shell Programming and Scripting

Grep two lines at a time

Hello; i have a log file which had Invalid, error, missing words in it. I want to grab a line which matches either of the above words and one more line below the grepped line. Can this be done? I looked on other places on your forum, but there is nothing which is working. I tried... (6 Replies)
Discussion started by: joshjimda
6 Replies

10. Shell Programming and Scripting

grep time and date

Hi, I have a file which is a result of a script running every two minutes. What I wanted to do is to grep a specific date and time (hour and minute) from the file and then count the occurance of 201. I need to get the result of occurance of 201 every 5 minutes. What should I include in my... (8 Replies)
Discussion started by: ayhanne
8 Replies
Login or Register to Ask a Question