0

I have a pipe line that looks like this:

cat myfile | awk '$3 > 0 { print $1, $2 * $3 }' | tee >(printf) | perl myscript.pl

I would like to print the input to my Perl script using tee. How can I do this?

2
  • Is it OK for tee's output to appear on stderr? If tee's output must come on stdout, does it matter whether it appears before or after any output that the perl script might produce? Jan 19 '14 at 22:35
  • Ideally, I'd like to print the results of tee to stdout and have it appear before the results of my perl script.
    – turtle
    Jan 19 '14 at 22:42
2

The difficulty here is that when tee is invoked in the pipeline, its stdout has already been aimed at perl's stdin. The only way for it to know where stdout was aimed before the pipeline started is for you to dup stdout to another file descriptor, and to access that file descriptor via /dev/fd/n, if your OS supports that. So, this may be an option:

# dup fd 1 to another fd for the duration of this shell script.
# we'll use 4. any number higher than 2 is probably OK, but
# some coordination is required if a containing shell script
# decided to do the same thing.
exec 4>&1
cat myfile | awk '$3 > 0 { print $1, $2 * $3 }' | tee /dev/fd/4 | perl myscript.pl
1

If you want it printed to the terminal, then use /dev/tty. For more, see the manpage tty(4).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.