cmcl has asked for the wisdom of the Perl Monks concerning the following question:
However, I then have an issue with a broken pipe ('tac: write error: broken pipe'), since I close the filehandle while 'tac' is still writing to it with 'last', and this I do so that I stop reading the file once it falls out of the date range I'm interested in. If I take out 'last', then the error goes away, but it ends up reading the file to the end (or actually start of the file, since it is in reverse), which slows it down enormously, making it not viable. Is there a better way to do this, given the constraints I've mentioned? Perhaps a different way to invoke tac or an elegant way to terminate tac when I want to close the filehandle? Or some other way of doing this entirely? Any help appreciated! Cammy @loglist = nsort(@oldmsglogs); push(@loglist, "$basemsglog"); my @sortedlogs = (reverse @loglist); for $msglogfile (@sortedlogs) { if ($done eq 0) { $logfile = join('/', $baselogdir, $msglogfile); open(RDMESG, "-|", "tac", $logfile); while ( <RDMESG> ) { my $line = $_; if ($line =~ m/(^[A-Z][a-z][a-z]\s+\d{1,2}\s+\d{1,2}\:\d{1,2 +}\:\d{1,2})(.*)/) { my $logtime = $1; my $msg = $2; my $logutime = str2time("$logtime"); if ($logutime ge $unixstarttime) { if ($msg =~ m/kernel\:\s+CPU\d{1,3}\:\s+Package\s+temper +ature\s+above\s+threshold\,\s+cpu\s+clock\s+throttled/) { $ctcnt++; } } else { $done = 1; last; } } } close RDMESG; } else { if ($ctcnt > $threshold) { $ret = sprintf("$warnmsg %d", $ctcnt); return $ret; } else { return $ret; } } } if ($ctcnt > $threshold) { $ret = sprintf("$warnmsg %d", $ctcnt); return $ret; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: efficient way to read a file in reverse
by LanX (Saint) on Jan 06, 2021 at 13:50 UTC | |
by hippo (Archbishop) on Jan 06, 2021 at 14:06 UTC | |
by cmcl (Novice) on Jan 06, 2021 at 17:16 UTC | |
by LanX (Saint) on Jan 06, 2021 at 17:25 UTC | |
|
Re: efficient way to read a file in reverse
by haukex (Archbishop) on Jan 06, 2021 at 13:50 UTC | |
|
Re: efficient way to read a file in reverse
by tybalt89 (Monsignor) on Jan 07, 2021 at 15:13 UTC | |
by cmcl (Novice) on Jan 07, 2021 at 17:25 UTC | |
by tybalt89 (Monsignor) on Jan 07, 2021 at 21:05 UTC | |
by tybalt89 (Monsignor) on Sep 20, 2024 at 08:33 UTC |