2013-01-16 47 views
2

我试图筛选日志文件,并正在到的问题,我有什么到目前为止以下,这是不行的,尾-f,AWK并输出到文件>

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

目标为获取包含

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

一个文件并保存到accesscustom-filtered.log

ipaddress1 
ipaddress2 
ipaddress3 
ipaddress4 

其作品,未经输出访问custom-filtered.log,但>中的某些内容不能正常工作,并且文件最终为空。

编辑:改变了原来的例子是正确的

+3

我没看到那个'awk'命令的结束单引号(''')。这是你有什么? – chrisaycock

+0

tail -f是一个非终止命令,所以我不确定你可以通过管道将它输出到像 –

+1

那样的文件中,你可以将它变成很时髦的东西。 – peteches

回答

4

使用tee

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

参见:Writing “tail -f” output to another fileTurn off buffering in pipe

注:awk不会在超级用户例如缓冲像grep ,所以你不需要用你的awk命令做任何特别的事情。 (more info

+0

从来没有听说过T恤,让我试试看...注意在我原来的帖子中的新命令,发布时忘记了一部分 – user1983916

+0

不起作用,我做了一个没有发球台的命令......而有一个命令发到了一个文件中。一个没有三通的输出正确的屏幕,一个没有添加到过滤文件 – user1983916

+0

看起来像第二个awk打破它,没有它看起来工作 – user1983916