Perl: reading from the tail -f pipe via STDIN

There were several other threads here, but the usual output was similar to "Install File :: Tail". But I am in the old box that we are decompiling, and I just want to write a one-line monitor to monitor the log. I tried installing File :: Tail, but the environment for CPAN just doesn't work, and I don't want to waste time figuring out what the problem is.

I just need a basic script that parses the IP address and counts it for me. However, for some reason, even this simple test does not work:

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

I think this has something to do with output buffering, but I have always been a bit fuzzy about how this works. How can I work with one liner?

+6
source share
3 answers

tail -f does not usually output a buffer, but grep probably does. Move grep functionality to your single line Perl:

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

man grep

 --line-buffered Use line buffering on output. This can cause a performance penalty. 

So:

 tail -f /log/file.txt | grep --line-buffered SomePattern | perl ... 
+2
source

Or without using a tail at all:

 perl -e 'open($h,$ARGV[0]); while (1) { /IPaddress/ and print "LINE: $_" for <$h>; sleep 1 }' snmplistener.log 
+1
source

Source: https://habr.com/ru/post/901143/


All Articles