Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

marked as duplicate by Gilles, warl0ck, jasonwryan, Michael Mrozek Sep 12 '12 at 12:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

possibly related SF question – jw013 Sep 11 '12 at 13:45
related to Upadte: replace first | by |&. Same problem here, that with | only STDOUT is piped... – user1146332 Sep 11 '12 at 14:18
Many programs only use colors when their output is a terminal, not when it's a pipe. If your program always outputs colors, see Removing control chars (including console codes / colours) from script output – Gilles Sep 12 '12 at 1:30
@Gilles, your script owns. It did it, now, would you mind providing it as an answer so I could accept it? – joltmode Sep 12 '12 at 7:07

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

share|improve this answer
Why running /etc/rc.d/ntpd stop returns without colors when USECOLOR="no", but rc.d stop ntpd returns with them? – joltmode 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'?

share|improve this answer

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'

share|improve this answer
It only did repositioning of some brackets. – joltmode Sep 11 '12 at 14:06

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