3

I'm trying to duplicate a command's output so I apply a post-treatment to one stream, then store it to a file while printing the original one on stdout.

Closest I could come with is:

command | tee /dev/stdout | sed 's/foo/bar/g' > out.txt

which does not work as the > captures both outputs.

I'd like to avoid using a temporary file if possible. Any clue?

wecx
  • 302
  • 2
  • 10
  • You could store the output in a bash variable with var=$(yor command) – terence hill Nov 19 '21 at 16:03
  • 2
    Does this answer your question? [How can I send the stdout of one process to multiple processes using (preferably unnamed) pipes in Unix (or Windows)?](https://stackoverflow.com/questions/60942/how-can-i-send-the-stdout-of-one-process-to-multiple-processes-using-preferably) – pjh Nov 19 '21 at 18:00

1 Answers1

3

Try

command | tee >(sed 's/foo/bar/g' > out.txt)

See ProcessSubstitution - Greg's Wiki for an explanation of >(...). In short, it causes tee to write to a path that pipes the written data as input to the sed 's/foo/bar/g' > out.txt command.

pjh
  • 6,388
  • 2
  • 16
  • 17