3

I have a command that is constantly running and prints a log in different colors to the terminal. And I'd like to redirect that commands output to a file, but also keep in the terminal.

So far this can simply be done with tee as in:

echo -e "\033[0;31mHello world\033[0m" 2>&1 | tee ./output.txt

But here comes the tricky part:

I don't want the color codes to appear in the file. And since my command runs over multiple hours at a time it can't be removed after the command has been executed, but it has to be removed while the command is still running.

If I run:

echo -e "\033[0;31mHello world\033[0m" 2>&1 | tee ./output.txt | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g"

then it removes the color for the terminal prints, but the color codes are still printed to the output file, so I kind of need to reverse this.

Any ideas how this could be done?

Forivin
  • 14,780
  • 27
  • 106
  • 199

0 Answers0