les*_*es2 27 bash grep cygwin cut pipe
我正在使用'tail -f'来跟踪日志文件,因为它已更新; 接下来,我将其输出传递给grep,以仅显示包含搜索词的行(在本例中为"org.springframework"); 最后我想把输出从grep传递到第三个命令'cut':
tail -f logfile | grep org.springframework | cut -c 25-
Run Code Online (Sandbox Code Playgroud)
如果可以从grep获取输入, cut命令将删除每行的前25个字符!(如果我从链中消除'grep',它会按预期工作.)
我正在使用cygwin和bash.
实际结果:当我添加第二个管道以连接到'cut'命令时,结果是它挂起,好像它在等待输入(如果你想知道).
Has*_*kun 29
假设GNU grep,添加--line-buffered到命令行,例如.
tail -f logfile | grep --line-buffered org.springframework | cut -c 25-
Run Code Online (Sandbox Code Playgroud)
编辑:
我看到grep缓冲不是这里唯一的问题,因为剪切不允许行式缓冲.
你可能想尝试用你可以控制的东西替换它,比如sed:
tail -f logfile | sed -u -n -e '/org\.springframework/ s/\(.\{0,25\}\).*$/\1/p'
Run Code Online (Sandbox Code Playgroud)
或者awk
tail -f logfile | awk '/org\.springframework/ {print substr($0, 0, 25);fflush("")}'
Run Code Online (Sandbox Code Playgroud)
Pau*_*ce. 11
在我的系统上,在得到任何输出之前,大约有8K被缓冲了.此序列立即跟踪文件:
tail -f logfile | while read line ; do echo "$line"| grep 'org.springframework'|cut -c 25- ; done
Run Code Online (Sandbox Code Playgroud)