-1

What am I doing wrong here? I don't get any STDOUT from tee but test.log is populated correctly.

perl test.pl | tee -i | sed 's/\x1b\[[0-9;]*m//g' > test.log

My goal is to sent everything printed by test.pl to STDOUT and dump a filtered version(using sed) of same to test.log

Jean
  • 21,665
  • 24
  • 69
  • 119
  • 1
    You're not giving a file name to `tee` – Robert Jul 13 '18 at 20:54
  • 1
    Also see [How to use Shellcheck](https://github.com/koalaman/shellcheck), [How to debug a bash script?](https://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](https://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](https://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jul 13 '18 at 21:07

3 Answers3

3

After clarification:

The solution by Daniel Schelper is the correct one on normal human usable shells (google Why never to use csh). Unfortunately sometimes we do not have a choice, so one solution:

perl test.pl | tee /dev/stderr | sed 's/\x1b\[[0-9;]*m//g' > test.log

What this does is duplicate stdout to stderr, and pipes only stdout forward. The output from perl will be displayed on screen (through stderr). This has the obvious caveat that any real errors may be hidden. A safer solution may be:

perl test.pl > /tmp/temp.log; cat /tmp/tmp.log & sed 's/\x1b\[[0-9;]*m//g' /tmp/tmp.log

which of course makes an extra file. The good options on csh usually involve more intermediate files compared to Bourne shells.


Old answer

tee should always be the last piped program (unless you want to log something midway I guess), and have a filename as an argument. The former is because the whole point is to see the output in the screen (not pass it on) and the second is because tee is usually not useful when it doesn't send output to a file as well.

As it currently stands, you are passing stdout through tee to the next pipe, sed, and that is dumping everything into test.log with the > operator. In fact, you should not have both > and tee:

perl test.pl | sed 's/\x1b\[[0-9;]*m//g' | tee test.log

to get results of sed both on screen and in your log. If I misunderstand and you in fact do want to log the results midway using tee

perl test.pl | tee mid.log | sed 's/\x1b\[[0-9;]*m//g' > test.log
kabanus
  • 24,623
  • 6
  • 41
  • 74
  • My goal is to send everything printed by `test.pl` to `STDOUT` and dump a filtered version of same to `test.log`. How do I do it then? – Jean Jul 13 '18 at 20:58
  • @DanielSchepler Just got back - do you want to write that as your own answer? – kabanus Jul 13 '18 at 21:03
1

If you're using Bash, i.e. Posix compliance of the script is not important, then you can use a process substitution:

perl test.pl | tee >(sed 's/\x1b\[[0-9;]*m//g' > test.log)
Daniel Schepler
  • 3,043
  • 14
  • 20
1

This might work for you:

perl test.pl | tee -i /dev/stderr | sed -f sed.sed > test.log

The perl program will pump its output to stdout. The tee will take the stdout and accept it as stdin and pump it out to stderr and stdout. The sed program will take the stdout and accept it as stdin and pump it out stdout which redirects it to test.log. The stderr will be directed to the terminal.

N.B. This may incur unwanted side-effects.

potong
  • 55,640
  • 6
  • 51
  • 83