Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve color when outputting with tee [closed]

Tags:

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?

like image 553
pap42 Avatar asked Nov 06 '12 16:11

pap42


2 Answers

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:

  1. regex borrowed from sampson-chen's answer.
  2. w command for /dev/stderr in sed is GNU sed's addition.
like image 159
anishsane Avatar answered Oct 08 '22 05:10

anishsane


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

like image 31
sampson-chen Avatar answered Oct 08 '22 04:10

sampson-chen