简体   繁体   English

tail -f,awk并输出到文件>

[英]tail -f, awk and output to file >

I am attempting to filter a log file and am running into issues, what I have so far is the following, which does not work, 我正在尝试过滤日志文件并遇到问题,到目前为止,以下内容是行不通的,

tail -f /var/log/squid/accesscustom.log | awk '/username/;/user-name/ {print $1; fflush("")}' | awk '!x[$0]++' > /var/log/squid/accesscustom-filtered.log

The goal is to take a file that contains 目标是获取包含以下内容的文件

ipaddress1 username
ipaddress7
ipaddress2 user-name
ipaddress1 username
ipaddress5
ipaddress3 username
ipaddress4 user-name

and save to accesscustom-filtered.log 并保存到accesscustom-filtered.log

ipaddress1
ipaddress2
ipaddress3
ipaddress4

It works without the output to accesscustom-filtered.log but something in the > isn't working right and the file ends up empty. 它没有accesscustom-filtered.log的输出就可以工作,但是>中的某些内容无法正常工作,并且文件最终为空。

Edit: Changed the original example to be correct 编辑:更改原始示例是正确的

Use tee : 使用tee

tail -f /var/log/squid/accesscustom.log | awk '/username/;/user-name/ {print $1}' | tee /var/log/squid/accesscustom-filtered.log

See also: Writing “tail -f” output to another file and Turn off buffering in pipe 另请参阅: 将“ tail -f”输出写入另一个文件关闭管道中的缓冲

Note: awk doesn't buffer like grep in the superuser example, so you shouldn't need to do anything special with your awk command. 注意:在超级用户示例中, awk不会像grep那样进行缓冲,因此您不需要使用awk命令做任何特殊的事情。 ( more info ) 更多信息

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

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