Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 587
coding_hero Avatar asked Nov 09 '11 19:11

coding_hero


1 Answers

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/'
like image 127
mob Avatar answered Sep 28 '22 09:09

mob