代码之家  ›  专栏  ›  技术社区  ›  DeborahAnn

过滤命令输出并打印到文件?

  •  0
  • DeborahAnn  · 技术社区  · 7 年前

    我现在正在推出这个bash系列 command -option | grep -A 1 --color 'string1\|string2' 过滤进程的输出。如何在文件上打印输出,而不是在控制台上打印过滤后的输出?

    我试过: command -option | grep -A 1 'string1\|string2' >> test.txt 但它没有打印任何文件。

    我还尝试添加正则表达式选项: command -option | grep -E -A 1 'string1|string2' >> test.txt

    1 回复  |  直到 7 年前
        1
  •  0
  •   DeborahAnn    7 年前

    显然,问题在于缓冲。通过逐行缓冲,问题得到了解决。

     command -option | grep --line-buffered -A 1 'string1\|string2' >> test.txt