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?

3 Answers 3


You can use a anonymous pipe for the second command:

texcount foo.tex | tee >(sed s/$/'\\\\'/ > wc.tex)
  • 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, 2012 at 13:41
  • tee , tee & tee.
    – bagavadhar
    Sep 10, 2012 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".

  • 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, 2011 at 11:49
  • 3
    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, 2011 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, 2012 at 13:45
  • @dubiousjim That semicolon is correct - the pipe works, but the semi colon better expresses what is intended. Sep 10, 2012 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.

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.