简体   繁体   English

如何通过awk和grep“过滤”尾部输出?

[英]How to “filter” a tail output throug awk and grep?

On a special console I like to filter some informations from /var/log/syslog. 在一个特殊的控制台上,我喜欢从/ var / log / syslog中过滤一些信息。 That was not really tricky: 这不是很棘手:

tail -f /var/log/syslog | awk '{print $2,$1,$9,$3,"\033[1;36m"$17 "\033[0m","\033[1;33m"$23 "\033[0m","\033[1;36m"$19 "\033[0m","\033[1;33m"$24 "\033[0m","\033[1;38m"$26"\033[0m","\033[1;32m"$13"\033[0m","\033[1;31m"$20 "\033[0m";}'

But now I want to pipe this through grep for a special field. 但现在我想通过grep管道这个特殊领域。 Just adding a "| grep Fieldname" does not work, even not grep first, awk later (which would make more sense). 只添加一个“| grep Fieldname”不起作用,即使不是grep first,awk稍后(这会更有意义)。

Could you please give me a tip? 你能给我一个提示吗?

Don't use grep , do the pattern match in awk . 不要使用grep ,在awk进行模式匹配。

tail -f /var/log/syslog | awk '/Fieldname/ {print $2,$1,$9,$3,"\033[1;36m"$17 "\033[0m","\033[1;33m"$23 "\033[0m","\033[1;36m"$19 "\033[0m","\033[1;33m"$24 "\033[0m","\033[1;38m"$26"\033[0m","\033[1;32m"$13"\033[0m","\033[1;31m"$20 "\033[0m";}'

If you really need to use grep , you can use the --line-buffered option so it doesn't buffer the output. 如果你真的需要使用grep ,你可以使用--line-buffered选项,这样它就不会缓冲输出。

tail -f /var/log/syslog | grep --line-buffered Fieldname | awk '{print $2,$1,$9,$3,"\033[1;36m"$17 "\033[0m","\033[1;33m"$23 "\033[0m","\033[1;36m"$19 "\033[0m","\033[1;33m"$24 "\033[0m","\033[1;38m"$26"\033[0m","\033[1;32m"$13"\033[0m","\033[1;31m"$20 "\033[0m";}'

If you want to grep the output of awk , you should use fflush() after printing each line to flush the buffer immediately. 如果你想grep的输出awk ,你应该使用fflush()打印每一行立即刷新缓冲区后。

tail -f /var/log/syslog | awk '{print $2,$1,$9,$3,"\033[1;36m"$17 "\033[0m","\033[1;33m"$23 "\033[0m","\033[1;36m"$19 "\033[0m","\033[1;33m"$24 "\033[0m","\033[1;38m"$26"\033[0m","\033[1;32m"$13"\033[0m","\033[1;31m"$20 "\033[0m"; fflush();}' | grep Fieldname

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM