Often I find myself doing tail -f file | grep --line-buffered "kernel" and your program has given me the inspiration to automate that. This is my version. It features taint mode and File::Tail:

#!/usr/bin/perl -T use strict; use warnings; use File::Tail; die "Not enough arguments\n" unless @ARGV; my ($file, $regex) = @ARGV; $regex = '.' unless defined $regex && length $regex; my $tail = File::Tail->new ( name => $file, maxinterval => 5, adjustafter => 10, ); $SIG{ALRM} = sub { printf "\n%s\n", '='x80; }; while (defined ($_ = $tail->read)) { print && alarm 2 if /$regex/; }

Let me anticipate that someone is going to reply talking about code interpolation in the regex:

# ~/bin/tf2.pl file '(??{print "Hello, world!\n"})' Eval-group not allowed at runtime, use re 'eval' in regex m/(??{print +"Hello, world!\n"})/ at /root/bin/tf2.pl line 20.

Update: Minor change according to imp's comment.

--
David Serrano


In reply to Re: tf - tail a file and output separator line when inactive by Hue-Bond
in thread tf - tail a file and output separator line when inactive by imp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.