I'm trying to get the output of a program (which has colors) and output it with tee
. I want the output to have colors on the terminal (stdout) but not on the output file. Is this possible?
A possible approach is, to print the escape characters on stderr, & main contents on stdout. I had done it in one of the script. It's of-course not a scalable option.
It would be an interesting exercise to write a dedicated script, which parses stdin, puts escape sequences on stderr & others on stdout. :-)
Then
./myScript.sh | filter_escapes | tee outfile.log
I have not seen any script which does that, but I think, it would be interesting to write one, if not already available.
As far as your question is concerned, I think below should suffice:
ls --color=always | sed -r 'w /dev/stderr' | sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' > /tmp/test
replace ls --color=always
with ./your_script & /tmp/test
with your intended output file name.
Other implementation:
ls --color=always | tee >(sed -r 's/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g' > /tmp/abcd)
Note: tee >(sed .... > logfile)
syntax
Note:
Yes, it's possible. Pipe the results you want to go into the output file through sed
to remove the escape characters used in formatting colors:
On Linux:
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
On OS X since it's not GNU sed:
sed -E "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
A part of a tool I wrote recently does exactly what you described with tee
:
https://github.com/sampson-chen/sack/blob/master/sack
ack --color $sack__flags $@ $sack__cwd | tee >$sack__dev_null >(display_shortcuts) >(process_shorcut_paths | remove_escaped_chars > $sack__shortcut_file)
where the function remove_escaped_chars
contains a check for OS version, then applies the sed
script as seen above.
(Note 1: tee
redirects a copy of output to stdout automatically, so I used >$sack__dev_null
to prevent that: because I wanted add additional information to the stuff printed to stdout, as defined in the function display_shortcuts
)
(Note 2: tee
itself definitely never removed the color formatting when I used it: My suspicion is other tools' default behaviour with pipes.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With