http://qs321.pair.com?node_id=11111692


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