我尝试将聊天日志文件通过管道传输到某些语音输出,并希望cut使用tr. 不幸的是,cut将它与tail -f结合使用时似乎停止了tr:
//works
$ tail /path/to/chatlogs | cut -b18-
test
test
//works, too
$ tail /path/to/chatlogs | tr -C -d '[:alnum:][:cntrl:] '
test
test
// does not work
$ tail -f /path/to/chatlogs | tr -C -d '[:alnum:][:cntrl:] ' | cut -b18-
//no output
Run Code Online (Sandbox Code Playgroud)
为什么?我该如何解决这个问题?
它甚至在管道两次进入时挂起cut:
$ tail -f file | cut -b5- | cut -b5-
//no output
Run Code Online (Sandbox Code Playgroud)
您使用的语法仅处理 上的新输入/path/to/chatlogs。尝试运行该命令并记录一个新条目/path/to/chatlogs并查看输出是什么或尝试:
tail -1000f /path/to/chatlogs | tr -C -d '[:alnum:][:cntrl:] ' | cut -b18-
Run Code Online (Sandbox Code Playgroud)
还处理最后 1000 行。
“挂起”部分实际上tail是等待进一步输入通过管道的过程。
要从tr命令中禁用缓冲,请使用:
tail -f /path/to/chatlogs | stdbuf -i0 -oL tr -C -d '[:alnum:][:cntrl:] ' | cut -b18-
Run Code Online (Sandbox Code Playgroud)
以上内容来自关闭管道中的缓冲