Bel*_*dez 41 grep bash rhel awk tail
我正在尝试grep
进行tail
文件日志并n
从一行中获取第th 个单词。示例文件:
$ cat > test.txt <<EOL
Beam goes blah
John goes hey
Beam goes what?
John goes forget it
Beam goes okay
Beam goes bye
EOL
^C
Run Code Online (Sandbox Code Playgroud)
现在,如果我做一个tail
:
$ tail -f test.txt
Beam goes blah
John goes hey
Beam goes what?
John goes forget it
Beam goes okay
Beam goes bye
^C
Run Code Online (Sandbox Code Playgroud)
如果我grep
认为tail
:
$ tail -f test.txt | grep Beam
Beam goes blah
Beam goes what?
Beam goes okay
Beam goes bye
^C
Run Code Online (Sandbox Code Playgroud)
但如果我awk
认为grep
:
$ tail -f test.txt | grep Beam | awk '{print $3}'
Run Code Online (Sandbox Code Playgroud)
不管我等多久都没有。我怀疑这与流的工作方式有关。
有人有任何线索吗?
cas*_*cas 67
它可能是来自 grep 的输出缓冲。你可以用grep --line-buffered
.
但是您不需要将 grep 的输出通过管道传输到 awk。awk 可以自己进行正则表达式模式匹配。
tail -f test.txt | awk '/Beam/ {print $3}'
Arc*_*ege 10
使用tail -f test.txt | awk '/Beam/{print $3}'
对我有用。以及使用tail -f test.txt | grep --line-buffered Beam | awk '{print $3}'
(gnu grep)。
这里的问题是是awk
逐行接收数据还是作为一个更大的数据块接收。grep 的 GNU 版本以更大的块发送输出,因为它更有效,但awk
需要逐行读取以便逐行输出。
这样说:grep
只会在缓冲区被填满时发送数据,awk 正在等待该缓冲区被填满,所以它什么都不发送。