Possible Duplicate:
Removing control chars (including console codes / colours) from script output

How do I disable all color codes when redirecting to file?

I previously asked how to redirect everything to file, now I want to know how to redirect without colors.

So instead of:

^[(B^[[m^[[1m^[[31m:: ^[(B^[[m^[[1mDaemon script ^[(B^[[m^[[1m^[[31mntpd^[(B^[[m^[[1m does not exist or is not executable.^[(B^[[m

I'm looking forward to achieve plain:

:: Daemon script ntpd does not exist or is not executable.


Looking at jw013 provided link, I tried the sed approach:

rc.d restart ntpd | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" &> progress.txt

But it didn't even redirect, and simply displayed the colored output to the console.

Update 2:

After user1146332 comment, in order to pipe STDERR, I have to pipe with |&.

rc.d restart ntpd | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" &> progress.txt

Resulted in:

^[(B:: ^[(BDaemon script ^[(Bntpd^[(B does not exist or is not executable.^[(B

It's tidier, but still some codes remain. But that sed RegExp is too hard for me to decypher.


Since you're running arch linux, you could export USECOLOR variable,

%> export USECOLOR="no"  # color-less
%> /etc/rc.d/ntpd stop

And the sed trick to strip off colors,

rc.d stop ntpd | sed -r "s:\x1B\[[0-9;]*[mK]::g"

But this can't handle backspaces generated from rc.d

  • Why running /etc/rc.d/ntpd stop returns without colors when USECOLOR="no", but rc.d stop ntpd returns with them? Sep 11 '12 at 15:04

One easy probably more universal method is to change the TERM variable to that does not support colors like maybe 'vt100'?


Wrote this on-the-fly, not sure about the format of your color codes but maybe give it a try:

command | awk '{gsub(/\^[^Bm]*[Bm]/,"")}1'

  • It only did repositioning of some brackets. Sep 11 '12 at 14:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.