Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a program texcount that outputs the number of words in my LaTeX document. I can also pipe the output of this to sed to make the newlines TeX linebreaks and write this to a file which I can then include in my final document. But when I do texcount foo.tex | sed s/$/'\\\\'/ > wc.tex the command line output of texcount is suppressed.

How can I get the output of the first command to be displayed in the terminal and piped to sed?

share|improve this question
up vote 9 down vote accepted

You can use a anonymous pipe for the second command:

texcount foo.tex | tee >(sed s/$/'\\\\'/ > wc.tex)
share|improve this answer
The essential part of this answer is to use tee. If you're certain shells like Bash, you can pipe tee's output using >(some further commands). In other shells, you'd have to give tee a filename argument (this is its standard mode of operation), and then run some further commands < thatfile, and then delete the thatfile. Or see Hari's answer below. – dubiousjim Sep 8 '12 at 13:41
tee , tee & tee. – Naai Sekar Sep 10 '12 at 18:22

You need the "tee" command, which allows you to split pipes.

texcount foo.tex | tee >output.txt | sed s/$/'\\\\'/ > wc.tex ; cat output.txt

This would leave you the additional output.txt file. Read this for more info: http://www.unixtutorial.org/2007/12/tee-replicate-standard-output/ You can also do "man tee".

share|improve this answer
texcount foo.tex | tee unmodified |sed s/$/'\\\\'/ > modified puts modified and unmodified versions of the output in the two files. How do I print the unmodified version to the terminal? I tried tee stdout but that prints nothing... – Seamus Jan 11 '11 at 11:49
You can't do that, because tee sends its input to stdout and a file. The stdout is what becomes the input to sed. You could add && cat unmodified at the end. That will dump the unmodifed output to the terminal. – KeithB Jan 11 '11 at 13:46
The proper command line here would be texcount foo.tex | tee output.txt | sed s/$/'\\\\'/ > wc.tex ; cat output.txt. Or you could use texcount foo.tex | tee output.txt; sed s/$/'\\\\'/ output.txt > wc.tex. In either case, you'd then probably want to rm output.txt afterwards. – dubiousjim Sep 8 '12 at 13:45
@dubiousjim That semicolon is correct - the pipe works, but the semi colon better expresses what is intended. – Danny Staple Sep 10 '12 at 9:19

You can use the fairly idiomatic

texcount foo.tex | tee /dev/tty | sed s/$/'\\\\'/ > wc.tex

/dev/tty is a magic device that refers to the controlling terminal of the current process.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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