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?
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?
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.
Comments