3

Trying to run a command that produces a lot of stdout and stderr. Need a way to filter most of the stderr messages.

Something like

command 1> grep "a" 2> grep "b"

alternative how to only grep stderr but also see stdout.

Pieter
  • 1,916
  • 17
  • 17

1 Answers1

1

Do:

{ command 2>&1 1>&3 3>&- | grep "b"; } 3>&1 1>&2 | grep "a"

Note: the above is adapted from antak's answer to "pipe stdout and stderr to two different processes in shell script?", with greps replacing its more abstract names. For how it works, see that answer. (Nevertheless, lazy coders who need grep might prefer a more specific answer like this.)

A useful util for distinguishing stdout from stderr is annotate-output, (install the "devscripts" package), which sends stderr and stdin both to stdout along with helpful little prefixes:

annotate-output wc -c /bin/bash /bin/nosuchshell

Output:

00:29:06 I: Started wc -c /bin/bash /bin/nosuchshell
00:29:06 E: wc: /bin/nosuchshell: No such file or directory
00:29:06 O: 1099016 /bin/bash
00:29:06 O: 1099016 total
00:29:06 I: Finished with exitcode 1

That output could be parsed separately using sed, awk, or even a tee and a few greps.

agc
  • 7,973
  • 2
  • 29
  • 50