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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |