Collecting all lines between two time stamp from the log


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Collecting all lines between two time stamp from the log
# 8  
Old 11-22-2015
I found the below perl script in other post, it is working perfectly for my requirements, since "from" and "to" time is unknown, can you help me to create the Perl script to take the current time for "from" and "-30" mins for "to".

Code:
perl -ne 'BEGIN {$from="150318 23:40:04" ; $to="150319  1:10:40"; $from=`date -d "$from" +%s`; $to=`date -d "$to" +%s`;} $r=`date -d "$1" +%s` if /^(.*?)\[/; print if ($r <= $to) && ($r >= $from);' file

looking similar in the perl
Code:
to=`date "+%y%m%d %H:%M:%S"`
from=`date "+%y%m%d %H:%M:%S" -d "-30 minutes"`

# 9  
Old 11-22-2015
My perl is quasi non-existent, but as you are running date from within it, try date +%s for now, and date -d -30min +%s for the other ?
# 10  
Old 11-22-2015
I tried the date in the perl and it showing invalid option.
# 11  
Old 11-22-2015
Quote:
Originally Posted by zenkarthi
I found the below perl script in other post, it is working perfectly for my requirements, since "from" and "to" time is unknown, can you help me to create the Perl script to take the current time for "from" and "-30" mins for "to".
I went back and read your first post and this will not do what you said your requirements are. Nevertheless, just so you believe me I will convert it to what you asked.

Code:
perl -ne '
    BEGIN{$from=qx(date -d -30min +%s); $to=qx(date +%s)}
    $r=qx(date -d "$1" +%s) if /^(.*?)\[/;
    print if ($r <= $to) && ($r >= $from);
' file

Even, if modified to accept a current time for to and 30 minutes ago for from, the condition if will not match any entries in your posted log. Substituting it for if /^(\d{6}\s+\d{2}:\d{2}:\d{2})/ it will match your time stamp format, but it will not print properly those entries in between that do not contain a time stamp.


Please, try the following:
Save as entries.pl and run as perl entries.pl log.txt

Code:
#!/usr/bin/perl

# this is not tested since I do not have any valid log files.
use strict;
use warnings;
use POSIX qw(strftime);

my $now = time;
my $ago = $now - (30*60);
my $from = strftime('%y%m%d %T', localtime($ago));
my $to = strftime('%y%m%d %T', localtime($now));

while(<>) {
    print if /$from/../$to/;
}


Last edited by Aia; 11-22-2015 at 05:19 PM..
This User Gave Thanks to Aia For This Post:
# 12  
Old 11-22-2015
Perfect , Thank you Aia for your help! Perl -ne syntax working well.

for the perl entries.pl log.txt returning to prompt, since it is working only if the exact /$from/ timestamp matching in the log.txt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script | Parse log file after a given date and time stamp

I am developing one script which will take log file name, output file name, date, hour and minute as an argument and based on these inputs, the script will scan and capture all the error(s) that have been triggered from a given time. Example: script should capture all the error after 13:50 on Jan... (2 Replies)
Discussion started by: ROMA3
2 Replies

2. Shell Programming and Scripting

Need Time Stamp Range On Log Files

I have created this script #!/bin/sh FILES=/data/log/access_*.log for f in $FILES do echo "Processing $f file" cat $f | awk '{print $1}' | sort | uniq -c | sort -n | tail done It produces this output Processing /data/log/access_abc.log file 114 1.1.1.1 167 2.2.2.2 ... (38 Replies)
Discussion started by: sharingsunshine
38 Replies

3. Shell Programming and Scripting

Collecting logs between two time stamps

Hi, please help me to collect the entire log files between two time stamp. for example, I am looking script to collect the entire log between "2015-03-27 15:59" to "2015-03-27 16:15" in the below sample log file. OS : RHEL 6.3 Date/Time : 24 hours format, the time is printing each log... (12 Replies)
Discussion started by: jerryknj
12 Replies

4. Shell Programming and Scripting

To check time stamp in log file and calculate.

Hi Friends, I have the following logfile. i want to make a script for calculate time by time2 - time1 1600266278|random|1|2014-09-19 02:08:56.024|2014-09-19 02:08:59.398|A|B|ROOM|Num0208559970111101788|1|dog|dos 1600266200|random|4|2014-09-19 02:08:06.572|2014-09-19... (2 Replies)
Discussion started by: ooilinlove
2 Replies

5. Shell Programming and Scripting

awk : collecting all data between two time frame

Hi Experts , I need your help to collect the complete data between two time frame from the log files, when I try awk it's collecting the data only which is printed with time stamp for example, awk works well from "16:00 to 17:30" but its not collecting <line*> "from 17:30 to 18:00" ... (8 Replies)
Discussion started by: zenkarthi
8 Replies

6. Shell Programming and Scripting

How to get time duration between two human readable time stamp in Unix?

Here is two time I have: Jul 12 16:02:01 Jul 13 01:02:01 and how can I do a simple match to get difference between two time which is 09:00:00 Thanks in advance. (3 Replies)
Discussion started by: ford99
3 Replies

7. Shell Programming and Scripting

Identify log files based on time stamp,zip and then copy..HELP

Hi All, PFB is a requirement. I am new to shell scripting. So plz help. It would be highly appreciated. 1. choose all the log files based on a particular date (files location is '/test/domain')--i.e,we should choose all the files that are modified on 29th November, neither 28th nor 30th 2.... (3 Replies)
Discussion started by: skdas_niladri
3 Replies

8. Shell Programming and Scripting

Remove lines of the same time stamp leaving the highest

Hi guys, I have a log that looks like that below. Columns 2 3 4 5 6 7 is the date/time stamp separated by comma. UUU,02,06,2010,10,00,00,00,0000000000000000,0000000000000000,0000000000001224 UUU,02,06,2010,10,05,00,00,0000000000000000,0000000000000000,0000000000001502... (2 Replies)
Discussion started by: borderblaster
2 Replies

9. Shell Programming and Scripting

Extract info from log file and compute using time date stamp

Looking for a shell script or a simple perl script . I am new to scripting and not very good at it . I have 2 directories . One of them holds a text file with list of files in it and the second one is a daily log which shows the file completion time. I need to co-relate both and make a report. ... (0 Replies)
Discussion started by: breez_drew
0 Replies

10. UNIX for Dummies Questions & Answers

Inserting Date&Time Stamp In Existing Log File

I am trying to insert a line with a date stamp in a file that is used to monitor activity in one of our directories. By doing this, I want to grep that file each day and go to the last entry for each time a error occurred and pull all errors generated if any exist. If error exists I want that error... (3 Replies)
Discussion started by: shephardfamily
3 Replies
Login or Register to Ask a Question