replicating stdout to stderr or stream 3

Posted on 2004-11-18
Medium Priority
Last Modified: 2013-12-26
I am wondering if there is a command to make a copy of stdout to say stderr.
I need this so I can transform each one differently.

I will call the command I an looking for replicate

(( echo "hello world" | replicate stdout,3 | sed -e "s/world/matt/g" ) 3>&1 1>&4 | sed -e "s/hello/brave new/g") 4>&1

I have found a solution for systems that have /dev/stderr and /dev/fd/3 etc
That is:
tee /dev/stderr
tee /dev/fd/3

I also found a solution in gawk, gawk supports the above discriptors internaly:
gawk  '{print $0; print $0 > "/dev/fd/3"}'

I am wondering if there is a standard command that can do it, where I dont need to write a program not even a tiny awk one.
Question by:bearware
LVL 14

Expert Comment

ID: 12615319
The standard way is with /dev/fd. If your system doesn't have /dev/fd, complain to the vendor.
LVL 51

Expert Comment

ID: 12626339
man tee

  command | tee -a file

if your shell support s file handles, file for tee can be a file handle, otherwise use named pipes

Author Comment

ID: 12642645
reply to ahoffmann:

If my a file handle you mean /dev/stderr /dev/fd/3 etc., then as noted in the question this only works on OSs that support it.

Using a shell that supports it makes no difference because it will not, and can not be expanded. The bash shell supports /dev/stderr for file redirection, e.g. >/dev/stderr >/dev/fd/3

If you have a different meaning please explain.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 51

Expert Comment

ID: 12653617
> if your shell support s file handles
I meant the shell (as written verbatim), not the underlaying OS or file system
/dev/fd/N is OS, but most shells have their own buil-in file descriptos (sh, bash, ksh, tcsh, etc. etc.)

I.g. it's a bad idea to rely on the OS devices like /dev/fd/3 (exception: /dev/null)

Author Comment

ID: 12653707
yes yes I dont want to rely on the underlying OS (because it cant do the job), So yes I am using only built ins of the shell.

Now ahoffmann says in his earlyer comment that I can do this with tee, A cant see how can you (ahoffmann) explain how this is done. I have no doubt that you know how, but can you explain to me.
LVL 51

Expert Comment

ID: 12657674
does following not do what you want?

  command | tee -a existing-file

Author Comment

ID: 12663082
N     N   OOOO
NN  N    O     O
N  NN    O     O
N    N    OOOO  I would not say that it did not if it did. (repeating your answer does not make it work)

Do you remember the question?

I Am trying to get the data to a stream e.i. >&3 but also to another stream >&4. This requires duplicating the data. Tee does the duplication put insists on puting it in a file. This is OK if it is run on an OS where there are files that corespont to these streams.
LVL 51

Expert Comment

ID: 12672798
nice shouting :-P

ok, you want filehandles and not files. Did you try with named pipes (see my first suggestion):
   mkfifo pipe3
   mkfifo pipe4
   exec 3< pipe3
   exec 4< pipe4
   command|tee -a pipe3 pipe4
# to be improved in many ways

Author Comment

ID: 13600766
I made a command as follows.: Done of the answers work as requested ( on a system that does not support special files (named pipes, speciel devices))

Thanks for trying to help.
gawk -- '
   print $0;
   print $0 > "/dev/stderr";


Accepted Solution

PAQ_Man earned 0 total points
ID: 15642486
PAQed with points refunded (125)

Community Support Moderator

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question