Home > Software design >  How can I suppress some output from a command using Bash?
How can I suppress some output from a command using Bash?

Time:11-03

I am trying to suppress some output from a command in bash, this is different to suppressing all output, because most of the output is useful, apart from a few lines that repeat hundreds of times which I would like to ignore.

Use-case: I have a CI server, at some point during the build, I am publishing a dotnet lambda function:

dotnet lambda package {...}

Most log output is useful, apart from a couple of logger calls from here and there. There is no way to suppress those via command-line switches to the command itself, which makes my build log much larger than I would like it to be:

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]

I would like to suppress output to standard out only if the line contains Changed permissions on published file or zipping. How do I do it?

CodePudding user response:

You can use grep -v to filter out lines on stdout containing the two search strings while printing all the rest. This does that and leaves stderr alone:

dotnet lambda package {...} | grep -v -e 'Changed permissions on published file' -e 'zipping'

You may also want to add --line-buffered to tell grep to print lines immediately and not buffer its output. This can be helpful if you're trying to view the log in realtime, but if the log is just going to disk to be viewed later I wouldn't use it since it'll make grep a little bit slower.

dotnet lambda package {...} | grep -v --line-buffered -e 'Changed permissions on published file' -e 'zipping'
  • Related