【问题标题】:Piping tail -f to cut to sed produces no output管道 tail -f 切到 sed 不产生输出
【发布时间】:2016-11-10 03:57:15
【问题描述】:

我尝试使用以下命令在终端中显示命名管道:

tail -f textFile | cut -d " " -f 3- | sed -e "s/a/g&g/"

由于某种原因,这不会产生任何输出。

如果 -f 被移除,它会按预期工作:

tail textFile | cut -d " " -f 3- | sed -e "s/a/g&g/"

或删除cut语句:

tail -f textFile | sed -e "s/a/g&g/"

或删除 sed 语句:

tail -f textFile | cut -d " " -f 3-

只有当这三个东西都在一起时,它才突然不再产生任何输出。 sed 和 cut 的顺序没有区别。所有这一切让我很难责怪任何一个或一对这些程序的输入或输出缓冲行为。

获得所需功能的可能解决方案是while read line 结构,但如果可能的话,我想避免为每一行初始化一个命令。

【问题讨论】:

  • 提供来自textFile的示例行,调查原因!
  • 真的没关系。出于测试目的,我使用了一个包含以下内容的文本文件:aaaaaaaaaaaaaaaaaaaaa \n bbbbbbbbbbbbbbbb
  • 你可以试试tail -f textFile | while read text; do echo "$text" | cut -d " " -f 3- | sed -e "s/a/g&g/; done 这个命令吗?因为,我猜这是tailcut 分别处理它们的输出和输入流的方式!如果可行,将其作为答案发布
  • 这行得通(如果你添加了缺失的“),但我已经解释了为什么我宁愿在我的原始帖子中避免这个解决方案。

标签: linux shell sed cut tail


【解决方案1】:

我在想要过滤的 ping 命令中遇到了类似的情况。

以下网页似乎解释了问题所在(stdio 缓冲) http://www.pixelbeat.org/programming/stdio_buffering/

该网站指出了一个解决方案,该解决方案涉及使用“stdbuf”命令禁用缓冲

tail -f filename | stdbuf -o0 cut -d " " -f 3- | sed -e "s/a/g&g/"

以上对我来说效果很好,删除“stdbuf -o0”会导致不显示任何输出。

>stdbuf --help
Usage: stdbuf OPTION... COMMAND
Run COMMAND, with modified buffering operations for its standard streams.

Mandatory arguments to long options are mandatory for short options too.
  -i, --input=MODE   adjust standard input stream buffering
  -o, --output=MODE  adjust standard output stream buffering
  -e, --error=MODE   adjust standard error stream buffering
      --help     display this help and exit
      --version  output version information and exit

【讨论】:

  • 您提到的文章提到使用-oL 进行面向生产线的吞吐量(尾部和切割产生,afaik)您选择-o0 的任何原因?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 2022-01-27
相关资源
最近更新 更多