Re: extract last 1 min log and find a string
by karlgoethebier (Abbot) on Jul 20, 2017 at 19:26 UTC
|
#!/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
| [reply] [d/l] [select] |
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?
| [reply] |
|
|
| [reply] |
|
|
| [reply] [d/l] [select] |
|
|
|
|
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.
| [reply] |
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! | [reply] |
|
|
| [reply] |
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;}
}
| [reply] [d/l] |
|
|
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 | [reply] |
|
|
Hi Dude,
I just copied an extra "}", why not fix it at your end?
| [reply] |
|
|
|
|
| A reply falls below the community's threshold of quality. You may see it by logging in. |