如何使用 Bash 抑制命令的某些输出?
How can I suppress some output from a command using Bash?
我试图抑制 bash 中命令的 一些 输出,这与 suppressing all output 不同,因为大部分输出都是有用的,除了重复数百次的几行我想忽略。
用例:我有一个 CI 服务器,在构建过程中的某个时刻,我发布了一个 dotnet lambda 函数:
dotnet lambda package {...}
除了来自 here and there 的几个记录器调用之外,大多数日志输出都是有用的。无法通过命令行切换到命令本身来抑制这些,这使得我的构建日志比我希望的要大得多:
48.67 ... publish: FooBar.Client -> {...}/FooBar.Client.dll <<< Useful
49.29 ... publish: FooBar -> {...}/bootstrap.dll
49.71 ... publish: FooBar -> {...}/publish/
49.73 Changed permissions on published file (chmod +rx appsettings.json). <<< Meh
[Repeated many times for other files]
#12 49.91 ... zipping: adding: appsettings.json (deflated 29%) <<< Meh
[Repeated many times for other files]
我想仅在该行包含 Changed permissions on published file
或 zipping
时才禁止输出到标准输出。我该怎么做?
您可以使用 grep -v
过滤掉标准输出中包含两个搜索字符串的行,同时打印所有其他字符串。这样做并单独留下 stderr:
dotnet lambda package {...} | grep -v -e 'Changed permissions on published file' -e 'zipping'
您可能还想添加 --line-buffered
以告诉 grep 立即打印行而不缓冲其输出。如果您尝试实时查看日志,这会很有帮助,但如果日志只是转到磁盘以便稍后查看,我不会使用它,因为它会使 grep 变慢一点。
dotnet lambda package {...} | grep -v --line-buffered -e 'Changed permissions on published file' -e 'zipping'
我试图抑制 bash 中命令的 一些 输出,这与 suppressing all output 不同,因为大部分输出都是有用的,除了重复数百次的几行我想忽略。
用例:我有一个 CI 服务器,在构建过程中的某个时刻,我发布了一个 dotnet lambda 函数:
dotnet lambda package {...}
除了来自 here and there 的几个记录器调用之外,大多数日志输出都是有用的。无法通过命令行切换到命令本身来抑制这些,这使得我的构建日志比我希望的要大得多:
48.67 ... publish: FooBar.Client -> {...}/FooBar.Client.dll <<< Useful
49.29 ... publish: FooBar -> {...}/bootstrap.dll
49.71 ... publish: FooBar -> {...}/publish/
49.73 Changed permissions on published file (chmod +rx appsettings.json). <<< Meh
[Repeated many times for other files]
#12 49.91 ... zipping: adding: appsettings.json (deflated 29%) <<< Meh
[Repeated many times for other files]
我想仅在该行包含 Changed permissions on published file
或 zipping
时才禁止输出到标准输出。我该怎么做?
您可以使用 grep -v
过滤掉标准输出中包含两个搜索字符串的行,同时打印所有其他字符串。这样做并单独留下 stderr:
dotnet lambda package {...} | grep -v -e 'Changed permissions on published file' -e 'zipping'
您可能还想添加 --line-buffered
以告诉 grep 立即打印行而不缓冲其输出。如果您尝试实时查看日志,这会很有帮助,但如果日志只是转到磁盘以便稍后查看,我不会使用它,因为它会使 grep 变慢一点。
dotnet lambda package {...} | grep -v --line-buffered -e 'Changed permissions on published file' -e 'zipping'