1

I want to output a command's results to both the console and a file. For that, I use tee. But I would also like to remove the ESC character from the file and not from the console output because it removes the colouring. I have tried using sed -e 's/\x1b\[[0-9;]*m//g', which removes the colouring and ESC from both.

1 Answer 1

1

I would probably capture the file and then, when it's done, post-process the file to remove the unwanted characters.

If you need to do it on the fly, and if you use a shell with process substitutions (>(...)), then you could use one of these to filter the file output of tee:

some-command | tee >( sed -e 's/\x1b[[0-9;]m//g' >file )

Assuming some-command still outputs coloured text when it's not connected to a terminal, this would filter the output through your sed command before passing the modified output on to the file called file. The unmodified output of some-command is also passed to the standard output of tee as expected.

Without process substitutions, this could be with sed alone like so:

some-command | sed -n -e p -e 's/\x1b[[0-9;]m//g' -e 'w file'

This uses sed to output the unmodified line to standard output (with p) and then modifies with your substitution expression. In the end, the modified line is written to file with the w command. The default output of the modified line at the end of the editing script is turned off with -n.

You must log in to answer this question.

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