Search code examples
perlstdintailoutput-buffering

Perl: Reading from a 'tail -f' pipe via STDIN


There were a number of other threads like this, but the usual conclusion was something like "Install File::Tail". But, I'm on an old box that we're decomissioning, and I just want to write a one-liner to monitor a log. I tried installing File::Tail, but the environment for CPAN just isn't working, and I don't want to take the time to figure out what the problem is.

I just want a basic script that parses out an IP address and keeps a count of it for me. For some reason, though, even this simple test doesn't work:

$ tail -f snmplistener.log|grep IPaddress |perl -ne 'print "LINE: $_\n";'

I think it has something to do with output buffering, but I've always been a bit fuzzy on how that works. How can I get this one-liner working?


Solution

  • tail -f doesn't generally buffer output, but grep probably does. Move the "grep" functionality into your Perl one-liner:

    tail -f snmplistener.log | perl -ne 'print "LINE: $_\n" if /IPaddress/'