多个管道使文本流中的输出为空白

More than one pipe makes output blank in streams of text

这个问题与 this 无关,因为我不想将输出重定向到多个进程。相反,我想像往常一样通过管道输出两次,如下所示:

$ echo "piping twice" | cut -d 'p' -f 3 | cut -d ' ' -f 1
ing

这符合预期! 但是,尝试与 tail -f 相同失败:

$ echo "piping twice" > somefile

# The first test passes: 
$ tail -f somefile | cut -d 'p' -f 3
ing twice

# The second test shows nothing:
$ tail -f somefile | cut -d 'p' -f 3 | cut -d ' ' -f 1

我使用 tail 作为一个可重现的例子,但我实际上是在尝试解析 youtube-dl 的进度输出,并且不止一个管道总是导致空行。
我做错了什么吗?
谢谢。

这会起作用:

tail -f somefile | stdbuf -oL cut -d 'p' -f 3| cut -d ' ' -f 1

如果 stdout 连接到 TTY,大多数 Linux 程序将使用行缓冲,否则将使用全缓冲。您可以使用 stdbuf 强制行缓冲。