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

Re: extract the tail from a string (with new lines) containing a substring

by johngg (Canon)
on Jan 21, 2020 at 15:54 UTC ( [id://11111692]=note: print w/replies, xml ) Need Help??


in reply to extract the tail from a string (with new lines) containing a substring

I've been giving this problem some thought and I think a technique from the O'Reilly Perl Cookbook might be applicable. Instead of shelling out to /usr/bin/grep you could open file-B for reading inside your script. You could then read the file line by line in a while ( <$filehandle> ) { ... } loop until end of file, using Perl's grep or, more likely, simple pattern matching to select lines to print to file-A. Once EOF has been reached you could then sleep for 20 minutes, without closing the filehandle, before using seek on awakening to reset the error condition and continue reading exactly where you left off.

The file rotation throws a few wrinkles into things but they should not be insurmountable. If the file is rotated by renaming the old file (e.g. to file-B.1 etc.) and creating a new file-B the script will still have the original file open and can read the remaining lines before close'ing that and open'ing and processing the new log file. This could be detected by doing a stat on the file when opening it and cacheing the device and inode, checking whether the current file-B's device and inode has changed. If the file is not rotated but is just truncated instead then you could detect this by doing a tell on the filehandle before sleeping and checking that the log file on awakening is now smaller than previously. That assumes that the file doesn't hit the 10MB limit in the 20 minute time frame.

All of the above would be within an endless while ( 1 ) { ... } loop so you would install signal handlers for INT, TERM and QUIT signals so that the script can terminate tidily.

I hope these thoughts are of interest and are helpful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: extract the tail from a string (with new lines) containing a substring
by jjmoka (Beadle) on Jan 22, 2020 at 22:53 UTC
    Thanks for the good ideas and for your time

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found