[英]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". 还有很多其他类似的线程,但通常的结论是“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.
我尝试安装File :: Tail,但CPAN的环境不起作用,我不想花时间弄清问题是什么。
I just want a basic script that parses out an IP address and keeps a count of it for me. 我只想要一个基本脚本来解析一个IP地址,并为我保留一个计数。 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?
我怎样才能使这个单线工作?
tail -f
doesn't generally buffer output, but grep
probably does. tail -f
通常不会缓冲输出,但grep
可能会这样做。 Move the "grep" functionality into your Perl one-liner: 将“grep”功能移动到Perl one-liner中:
tail -f snmplistener.log | perl -ne 'print "LINE: $_\n" if /IPaddress/'
man grep 男人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 ...
或者根本不使用尾巴:
perl -e 'open($h,$ARGV[0]); while (1) { /IPaddress/ and print "LINE: $_" for <$h>; sleep 1 }' snmplistener.log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.