in reply to Re: Regex to find the last matching set in a long scalar
in thread Regex to find the last matching set in a long scalar
If the log file is huge, it will be better to avoid the scalar variable altogether and to instead read the file in backwards, line by line. The File::ReadBackwards module on is designed for this purpose:
#! perl use strict; use warnings; use File::ReadBackwards; my $logfile = 'log.txt'; my $header = 'start: real'; my $footer = 'end: real'; my $in_data = 0; my @lines; my $bw = File::ReadBackwards->new($logfile) or die "Cannot open file '$logfile' for reading backwards: $!"; while (my $line = $bw->readline) { chomp $line; if ($in_data) { unshift @lines, $line; last if $line eq $header; } elsif ($line eq $footer) { unshift @lines, $line; $in_data = 1; } } print join("\n", @lines), "\n";
No regex needed. :-)
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Regex to find the last matching set in a long scalar
by Laurent_R (Canon) on Nov 16, 2014 at 14:17 UTC |