Home > front end >  How to print stream multiple times?
How to print stream multiple times?

Time:03-03

cat ciao.py | tee >(xargs echo) | xargs echo

and the output is:

print(ciaoo) print(ciaoo)

I'd like to print the stream two times using only one starting stream. How can I do that? echo and printf seems not work. Is there another way to do it without xargs? A cleaner way?

Also, how can I print them in new line like these?

print(ciaoo)
print(ciaoo)

EDIT: I know I can do sed 's/.*/&\n&/' ciao.py but instead I want to do that via pipe stream, maybe could be useful in certain scenarios.

Also, if I do cat ciao.py | sed 's/.*/&\n&/' it would work, but I want to do this in two separated times, simulating the case where I want to print it and then pass that to further commands.

CodePudding user response:

You can do so using sed

$ sed 's/.*/&\n&/' ciao.py
print(ciaoo)
print(ciaoo)

CodePudding user response:

In order to print the stream twice, you need to save a copy somewhere temporarily, then print the copy when the first pass is finished. Something like this:

produceStream | { tempfile=$(mktemp); tee "$tempfile"; cat "$tempfile"; rm "$tempfile"; }
  • Related