tpipe | What if you want to use the output of a program twice, and you don't want to deal with an intermediary file? Try the tpipe program. |
---|
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 %%Pages:
at the end of
the file.
Using
gzcat (24.7)
to uncompress the file
to standard output, I can type the following commands into a
for loop (9.12)
(or put them into a shell script).
This loop sends each
file to the printer and uses sed to capture the correct line:
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.
-