13.11 tpipe-Redirecting stdout to More than One Place
tpipe is similar to tee ( 13.9 ) , but instead of putting a copy of standard input in a file, it passes the input to a new pipe. You could simulate tpipe by using tee and running the commands on the tee file, but there are instances when you don't want to clutter up your disk with files.
For example, suppose I have some large, compressed PostScript files.
I want to print the files, but I also want to
know how many pages they are.
I know that the number of pages
appears on a line following
for f do gzcat $f | lpr gzcat $f | sed -n "s/^%%Pages: \([0-9][0-9]*\)/$f: \1 pages/p" done But this ends up running gzcat twice, which takes some time. I can gunzip the file first, but frankly I'm not sure I have the disk space for that. Using tpipe , I can do it in one line, without wasting processes and without eating disk space:
for f do gzcat $f | tpipe lpr | sed -n "s/^%%Pages: \([0-9][0-9]*\)/$f: \1 pages/p" done From running this script, as each file is sent to the printer I receive the following messages on my screen:
ch01.ps.gz: 44 pages ch02.ps.gz: 51 pages ch03.ps.gz: 23 pages ... If you don't have tpipe , you can also simulate it using awk ( 33.11 ) :
gzcat $f | awk "{ print | \"lpr\" ; print }" | \ sed -n "s/^%%Pages: \([0-9][0-9]*\)/$f: \1 pages/p" This is much slower and only works on text files, but it does the job. - |
|