Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: grep lines from log for last 10 mints

by Anonymous Monk
on Aug 06, 2012 at 10:11 UTC ( #985645=note: print w/ replies, xml ) Need Help??


in reply to Re: grep lines from log for last 10 mints
in thread grep lines from log for last 10 mints

So something resembling this

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump; my $command = q{awk} .q{ -v start="$(date -d '10 mins ago' +'%b %e %T')"} .q{ -v end="$(date +'%b %e %T')"} .q{ '} #### ???? .q{ {tm=$1" "$2" "$3}} .q{ ; } .q{ start<tm } .q{ && } .q{ tm<end} .q{ '} .q{ /d/d1/logs/stdout-1132.txt}; dd $command; my $line = qx{$command}; dd $line;

If I knew awk, I'd still recommend converting it to perl using a2p


Comment on Re^2: grep lines from log for last 10 mints
Download Code
Re^3: grep lines from log for last 10 mints
by Anonymous Monk on Aug 06, 2012 at 10:30 UTC
    I think I figured out how to a2p
    #!perl eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if $running_under_some_shell; # this emulates #! processing on NIH machines. # (remove #! line above if indigestible) eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift; # process any FOO=bar switches while (<>) { @Fld = split(' ', $_, -1); $tm = $Fld[(1)-1] . ' ' . $Fld[(2)-1] . ' ' . $Fld[(3)-1]; ; print $_ if $start lt $tm && $tm lt $end; #??? #??? } Please check my work on the 2 lines I've marked with "#???". The operation I've selected may be wrong for the operand types.

    Massaging that a little and adding start/end

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; my $start = qx{date +'%b %e %T'}; my $end = qx{date -d '10 mins ago' +'%b %e %T'}; dd $start, $end; while (<>) { my @Fld = split(' ', $_, -1); my $tm = $Fld[(1)-1] . ' ' . $Fld[(2)-1] . ' ' . $Fld[(3)-1]; print $_ if $start lt $tm and $tm lt $end; }
    Use as  ./tminus10 path/to/logfile
      i tried to run below code by passing path of log but getting below error continuous for line "my $tm = $Fld(1)-1 . ' ' . $Fld(2)-1 . ' ' . $Fld(3)-1;"
      #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; my $start = qx{date +'%b %e %T'}; my $end = qx{date -d '10 mins ago' +'%b %e %T'}; dd $start, $end; while (<>) { my @Fld = split(' ', $_, -1); my $tm = $Fld[(1)-1] . ' ' . $Fld[(2)-1] . ' ' . $Fld[(3)-1]; print $_ if $start lt $tm and $tm lt $end; }
      Use of uninitialized value in concatenation (.) or string at ./test.pl line 11, <> line 144807. Use of uninitialized value in concatenation (.) or string at ./test.pl line 11, <> line 144807.

        So?

        FWIW, its a warning, if you don't want warnings, turn them off

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985645]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-10-25 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls