When I run an arbitrary command in the terminal in linux, is there a way to suppress output messages that contain a certain sentence?
I tried
./mycommand | grep -v "I dont want to see this"
but the messages were still there.
Maybe the unwanted part is part of the output to stderr but to stdout.
Try:
./mycommand 2>&1 | grep -v "I dont want to see this"
You can pipe stderr and stdout to different targets. So you may see where the output comes from:
./mycommand > >(grep -v "I dont want to see this" > stdout.log) 2> >(grep -v "I dont want to see this" > stderr.log)
./mycommand 2>&1 | grep -v -e '^$' -e 'unwanted message'
where -e '^$'
checks for empty lines and -e 'unwanted message'
checks for the messages that I want to suppress.
Mar 16, 2016 at 2:10
To add to cmks' answer, if you also want the return code to be the one from mycommand
and not from grep
, you can use pipefail
and ignore the return status from grep
(for when the string to suppress isn't found)
(set -o pipefail; (./mycommand 2>&1) | { grep -v "I dont want to see this" || true; })
Examples:
(set -o pipefail; (echo "REMOVE" && false 2>&1) | { grep -v "REMOVE" || true; })
1
(set -o pipefail; (echo "REMOVE" && true 2>&1) | { grep -v "REMOVE" || true; })
0
(set -o pipefail; (echo "KEEP" && false 2>&1) | { grep -v "REMOVE" || true; })
KEEP
1
(set -o pipefail; (echo "KEEP" && true 2>&1) | { grep -v "REMOVE" || true; })
KEEP
0
./mycommand |& grep -v "I dont want to see this"
?