How to pipe output of command to another command while also displaying it on the console?

Zain R

I want to write part of the results of a stream to a file, but I want the entire contents of the stream printed to the console. Is there some command that would help with this?

Minimal example:

Say I had a file foo.txt with contents:

bat
dude
rude

And I wanted to write all lines in that file which contain the letter 'a' to bar.txt. I could write

cat foo.txt | grep 'a' > bar.txt

Which would result in bar.txt containing bat. But that wouldn't give me the console output that I want.

Instead I would prefer something like:

cat foo.txt | output-stdin-to-console-and-pass-to-stdout | grep 'a' > bar.txt

Which would not only write bat to bar.txt but also write the following to the console:

bat
dude
rude

Is there any command I can run to do that?

Kamil Maciorowski

Explicit examples with tee:

  • tee writing to the tty

    < foo.txt tee /dev/tty | grep 'a' > bar.txt
    

    This is portable, works in sh.

  • tee writing to process substitution, its standard output goes to the console:

    < foo.txt tee >(grep 'a' > bar.txt)
    

    This is not portable, works in Bash and few other shells.

Note I got rid of the cat command (useless use of cat).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Highlight console search output, while displaying entire command output

From Dev

Bash: How to get stdout to console and also pipe to next command?

From Dev

How to pipe a single item from a command output into another command?

From Dev

How to pipe single result from output of locate command to another command?

From Dev

How to pipe data to interactive bash script and pipe output to another command?

From Dev

Bash: How to direct output to both stderr and to stdout, to pipe into another command?

From Dev

Pipe command output to Yad and also log the output to a logfile

From Dev

How to pipe output to sh script and pipe that to a command?

From Dev

zsh capture and pipe output to another command

From Dev

How do I pipe the output of a curl command to an environment variable and use it in another curl command?

From Dev

Powershell: Pipe external command output to another external command

From Dev

Powershell: Pipe external command output to another external command

From Dev

Pipe from a while loop to a command but execute another command if pipe command fails

From Dev

Pipe the output of a command to rm command

From Dev

pipe output into a delete command

From Dev

Pipe the output of a command if it is successful

From Dev

pipe output into a delete command

From Dev

pipe command output to convert?

From Dev

How to pipe command output to other commands?

From Dev

How to pipe a command output to multiple programs

From Dev

Perl: How to pipe output of an open command to a file

From Dev

How To Use A Command Output As An Argument of Another Command?

From Dev

How To Use A Command Output As An Argument of Another Command?

From Dev

Call Symfony console command within another command and suppress output

From Dev

How to send stderr to stdout with a pipe to another command?

From Dev

How to execute a system command that prints in color while capturing output, but can also be cancelled in Ruby?

From Dev

Windows: Command line redirection to text file while also seeing output

From Dev

Win10 - How to pipe the output from one command as an argument of another to check file hash?

From Dev

Python csv reader: how to pipe output to another script using command line

Related Related

  1. 1

    Highlight console search output, while displaying entire command output

  2. 2

    Bash: How to get stdout to console and also pipe to next command?

  3. 3

    How to pipe a single item from a command output into another command?

  4. 4

    How to pipe single result from output of locate command to another command?

  5. 5

    How to pipe data to interactive bash script and pipe output to another command?

  6. 6

    Bash: How to direct output to both stderr and to stdout, to pipe into another command?

  7. 7

    Pipe command output to Yad and also log the output to a logfile

  8. 8

    How to pipe output to sh script and pipe that to a command?

  9. 9

    zsh capture and pipe output to another command

  10. 10

    How do I pipe the output of a curl command to an environment variable and use it in another curl command?

  11. 11

    Powershell: Pipe external command output to another external command

  12. 12

    Powershell: Pipe external command output to another external command

  13. 13

    Pipe from a while loop to a command but execute another command if pipe command fails

  14. 14

    Pipe the output of a command to rm command

  15. 15

    pipe output into a delete command

  16. 16

    Pipe the output of a command if it is successful

  17. 17

    pipe output into a delete command

  18. 18

    pipe command output to convert?

  19. 19

    How to pipe command output to other commands?

  20. 20

    How to pipe a command output to multiple programs

  21. 21

    Perl: How to pipe output of an open command to a file

  22. 22

    How To Use A Command Output As An Argument of Another Command?

  23. 23

    How To Use A Command Output As An Argument of Another Command?

  24. 24

    Call Symfony console command within another command and suppress output

  25. 25

    How to send stderr to stdout with a pipe to another command?

  26. 26

    How to execute a system command that prints in color while capturing output, but can also be cancelled in Ruby?

  27. 27

    Windows: Command line redirection to text file while also seeing output

  28. 28

    Win10 - How to pipe the output from one command as an argument of another to check file hash?

  29. 29

    Python csv reader: how to pipe output to another script using command line

HotTag

Archive