Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 重定向tail-f+;grep/awk?_Linux_Shell_Awk - Fatal编程技术网

Linux 重定向tail-f+;grep/awk?

Linux 重定向tail-f+;grep/awk?,linux,shell,awk,Linux,Shell,Awk,我有一个不断增长的test.log,如下所示: abc ID1 aaa ID2 abb ID3 ccc ID4 我想像这样将“*a.b.”的对应ID保存到日志文件 $ tail -f test.log | grep --line-buffered '.*a.*b.*' | awk '{print $2}' > a_ID.log 我试过了 $ tail -f test.log | grep '.*a.*b.*' $ tail -f test.log | grep --line-buff

我有一个不断增长的test.log,如下所示:

abc ID1
aaa ID2
abb ID3
ccc ID4
我想像这样将“*a.b.”的对应ID保存到日志文件

$ tail -f test.log | grep --line-buffered '.*a.*b.*' | awk '{print $2}' > a_ID.log 
我试过了

$ tail -f test.log | grep '.*a.*b.*'
$ tail -f test.log | grep --line-buffered '.*a.*b.*' > a.log
两者都很好,但是awk应该怎么做呢

# No output
$ tail -f test.log | awk '{print $2}'
# Obviously nothing in ID.log
$ tail -f test.log | awk '{print $2}' > ID.log
awk是否有像grep一样的'--line缓冲'?sed怎么样?

你能试试吗

$ tail -f test.log | awk '{ print $2; fflush(); }'

从手册页上看,它说它将冲洗stdout。

这似乎与mawk有关

$ tail -f test.log | awk -W interactive '{print $2}' > ID.log

-W interactive
使mawk write无缓冲地写入stdout。

中详细讨论了这一点,但简短的是,在这种情况下不需要无缓冲地写入Awk。还有别的问题。尽管如此,还是要阅读FAQ页面。还要注意,
grep.*a.*b.*
写得更好
grep'a.*b'
。thx很多,我会阅读它我的awk是mawk,而不是GNU awk,这就是它失败的原因。但无论如何都是thx。