9

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

9

You can use a anonymous pipe for the second command:

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

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.

1
  • Simplest is best, and /dev/tty — great to learn. Commented Jul 12 at 17:58
7

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".

4
  • 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
    Commented 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
    Commented 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
    Commented Sep 8, 2012 at 13:45
  • @dubiousjim That semicolon is correct - the pipe works, but the semi colon better expresses what is intended. Commented Sep 10, 2012 at 9:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .