How to print lines from a log file that occurs after a certain time

I want to print all the lines that allow me to say that they occur after some time, the value of which is returned by the function localtime perlinside a perlscript. I tried something like below:

my $timestamp = localtime();
open(CMD,'-|','cat xyz.log | grep -A1000 \$timestamp' || die ('Could not open');
while (defined(my $line=<CMD>)){
    print $line;
}

If I replaced the $timestampin command with the catactaul time component of xyz.log, then it prints the lines but does not print with the variable $timestamp.

Is there an alternative way to print lines that appear after the current time in the log files, or how can I improve the command above?

+4
source share
4 answers

$timestamp Perl, . , ? Perl .

, . script.

, . , , .

while (<$fh>) { last if /$timestamp/ }

print while <$fh>;

, , $timestamp . , , .

- , , , .

use warnings 'all';
use strict;

my $timestamp = localtime();

my $logile = 'xyz.log';
open my $fh, '<', $logfile or die "Can't open $logfile: $!";

my $mark = 0;
while (<$fh>) 
{
    if (not $mark) { 
        $mark = 1 if /$timestamp/;
    }
    else { print }
}
close $fh;
+2

grepping Shell, perl , localtime:

 sed <xyz.log -ne"/^$(perl -E'say scalar localtime')/,\$p"

sed range: , , -n, ( ^ , ) ($) (p).

Perl :

my $timestamp = localtime();
my $found;
open(my $fh, '<', 'xyz.log') or die ('Could not open xyz.log: $!');
while (<$fh>) {
    if($found) {
        print;
    } else {
        $found = 1 if /^$timestamp/;
    }
}
+1

Perlish, :

open (my $cmd, "<", "xyz.log") or die $!;
#get all lines in an array with each index containing each line
my @log_lines = <$cmd>;

my $index = 0;

foreach my $line (@log_lines){
    #write regex to capture time from line
    my $rex = qr/regex_for_time_as_per_logline/is;
    if ($line =~ /$rex/){
        #found the line with expected time
        last;
    }
    $index++;
}

#At this point we have got the index of array from where our expected time starts.
#So all indexes after that have desired lines, which you can write as below

foreach ($index..$#log_lines){
    print $log_lines[$_];
}

, .

0

:

In this case, I tried to open / var / log / messages, then convert each line timestamp to an era and find all the lines that occurred after time ()

use Date::Parse;
my $epoch_now = time(); # print epoch current time.
open (my $fh, "</var/log/messages") || die "error: $!\n";

while (<$fh>) {
    chomp;
    # one log line - looks like this
    # Sep  9 08:17:01 localhost rsyslogd: rsyslogd was HUPed
    my ($mon, $day, $hour, $min, $sec) = ($_ =~ /(\S+)\s*(\d+)\s*(\d+):(\d+):(\d+)/);

    # date string part shouldn't be empty
    if (defined($mon) && defined($day)
        && defined($hour) && defined($min)
        && defined($sec)) {
        my $epoch_log = str2time("$mon $day $hour:$min:$sec");
        if ($epoch_log > $epoch_now) {
            print, "\n";
        }
    }
}
0
source

Source: https://habr.com/ru/post/1654089/


All Articles