ytjPerl has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks, I am writing a script to extract last 1 min log and search a string. I know I could use module like File::ReadBackwards, unfortunately, I do not have internet access on my VM and copying module manually with failure. So I am seeking for a way to avoid using external modules. Thanks!

  • Comment on extract last 1 min log and find a string

Replies are listed 'Best First'.
Re: extract last 1 min log and find a string
by karlgoethebier (Abbot) on Jul 20, 2017 at 19:26 UTC

    Some alternatives without using File::ReadBackwards:

    #!/usr/bin/env perl # $Id: reverse.pl,v 1.2 2017/07/20 18:51:47 karl Exp karl $ # http://perlmonks.org/?node_id=1195633 use strict; use warnings; use Tie::File; my $file = shift; print qx(cat $file); print qq(--\n); open my $fh, '<', $file or die $!; my @data = reverse <$fh>; close $fh; print for @data; print qq(--\n); tie( @data, "Tie::File", $file, mode => 0 ) or die $!; for ( reverse( 0 .. $#data ) ) { print qq($data[$_]\n); } untie @data; __END__ karls-mac-mini:monks karl$ ./reverse.pl file.txt foo bar nose cuke -- cuke nose bar foo -- cuke nose bar foo

    What you choose may depend on the size of your file. And everything has it's price.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: extract last 1 min log and find a string
by Laurent_R (Canon) on Jul 20, 2017 at 17:18 UTC
    AFAIK, File::ReadBackwards is a pure Perl module. You should be able to install it manually. There may be a couple of additional dependencies, but it should not be too difficult. What type of failure do you encounter?

      Please refer to the question I asked about .pm module location. "Can't locate File::/ReadBackwards.pm in @INC<@INC contains: D:/App/Perl/site/lib D:/App/Perl/lib.> I already did that. Thanks!

        You need to put the file ReadBackwards.pm in D:/App/Perl/site/lib/File. Create a sub folder File if necessary

        How big is the log file ? Gigabytes ?

        poj
        Yes, I saw that other question only after I saw this one and replied to it, but since it had the correct answer that you needed to create a File subdirectory, I did not think it was useful to further comment on that other question.
Re: extract last 1 min log and find a string
by ytjPerl (Scribe) on Jul 21, 2017 at 13:36 UTC
    Hey, folks. The log file actually is not that big. What I want to achieve is to match a string 'running mode' to make sure every thing is on the track. The file keeps updating quickly at the first 10 mins I think. So the solutions I could think of are:1 keep tracking every 2 min to search this string. 2 keep tailing 100 lines (this might not be working, because it is hard to catch the point which I tailed) 3 comparing the size of file until it is not increasing, extracting the bottom lines to match the string. Do you have any better solution? and what kind of module I could leverage in this case? Thanks!
      what kind of module I could leverage in this case?

      File::Tail should do the trick.

Re: extract last 1 min log and find a string
by ytjPerl (Scribe) on Jul 24, 2017 at 19:00 UTC
    I am think search the string from bottom line, I know I could use File:ReadBackwards, but how to achieve that when I find the first occurrence string, stop the search and print "found" out. My code below repeat print out "found" more than 4 times, but I already set $count = 4, please advice, thanks
    use File::stat; use Time::localtime; my $fh = "/daily20170718120401.log"; my $target = "48 processes started"; my $count == 0; while ( $count< 4){ my $timestamp = ctime(stat($fh)->mtime); print "$timestamp\n"; sleep(30); my $timestamp2 = ctime(stat($fh)->mtime); print "$timestamp2\n"; if ($timestamp eq $timestamp2) { my $bw = File::ReadBackwards->new("backwards.txt") or die $!; my $line; while (defined($line = $bw->readline)) { chomp $line; if ($line =~ /target/) { print "found\n"; $count = 4; } } $bw->close(); } } else{print "changed\n"; $count += 1;} }
      My code below repeat print out "found" more than 4 times

      That's odd, because I get

      syntax error at 1195905.pl line 31, near "else"
      syntax error at 1195905.pl line 31, near ";}"
      Unmatched right curly bracket at 1195905.pl line 33, at end of line
      1195905.pl had compilation errors.
      
      poj
        Hi Dude, I just copied an extra "}", why not fix it at your end?
A reply falls below the community's threshold of quality. You may see it by logging in.