0

Suppose I have a code, and it is running in a very verbose mode. This maximum verbosity exposes a message that I'd like to read to see how everything is doing. However, the terminal is flooded with other content.

Is there a way to grep-filter the stderror output without transitioning the stderror pipeline to the stdout pipeline?

for example,

my-command 2>&1 | grep something # ok, great; is it possible without the pipe shift?

Is this possible?

i.e., maybe
my-command 2>(grep something)?
Chris
  • 961
  • 7
  • 20
  • Looks like https://unix.stackexchange.com/q/3514/117549 would do it ...? – Jeff Schaller Apr 03 '20 at 16:23
  • Technically no in bash (I think other shells offer this) because you can only pipe stdout (“|”) or stdout and stderr (“|&”) to another process.

    The link @JeffSchaller provides is good, but I don't see the swapping stdout and stderr method. Are you interested in that?

    – Larry Apr 03 '20 at 21:19
  • 1
    Also the highest upvoted answer there would work $ { echo stdout; echo stderr >&2; } 2> >(cat -n) stdout 1 stderr – Larry Apr 03 '20 at 21:24
  • @Larry thanks for this -- hadn't considered using alternative shells. – Chris Apr 04 '20 at 14:09

1 Answers1

0

I'm putting these two options in as answers, even though they don't really answer the question (the answer is, “in bash, no”) in case someone else sees this, and to show the method of swapping stdout and stderr, and to have formatting correct (unlike in comments).

Answer one: suppose this is a program you want to run (I know it doesn't look like a command but pretend it is; replace all of this with the command and arguments you actually want to run):

{ echo stdout; echo stderr >&2; }

Now suppose you want to grep only stderr for something, but keep stdout as-is.

{ echo stdout; echo stderr >&2; } 2> >(grep something)

If that's all you did you'd get all of stdout, and parts of stderr that have something in it, all interspersed. You may want to write stdout and stderr to separate files for later viewing:

{ echo stdout; echo stderr >&2; } 2> >(grep something > errorfile) > outputfile

For swapping stdout and stderr (and why you'd want to do that) see https://stackoverflow.com/questions/13299317/io-redirection-swapping-stdout-and-stderr for a better explanation than I can give.

Larry
  • 257