Removing Color Codes From Output

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

I’m working on a script to work alongside a program that I’m writing. What i’m trying to do is assess the level of completion of a number of clients using another script. My script does pretty much exactly as I want, but there are some color constants that are inserted into my XML output, which mess things up when I’m parsing the XML into PHP later. long story short, I have a sed expression that I’m using which removes the first portion of the color constants, but it fails to remove the trailing escape sequence which looks something like ^[(B – thus my problem.

Here is the sed sequence I’m using:

sed -r 's/x1B[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g'

I’m wondering if there is a separate sed sequence I could run afterwards to remove the trailing sequence. I’ve tried using

sed 's/^[\(B//'

But it doesn’t seem to remove anything.
I don’t necessarily need an answer, if someone has a good guide on sed and color codes, that would also be super helpful. I’ve googled around, but the answers I’ve found just seem to remove the opening portion of the color.

Thanks for your help.

Asked By: SamHuckaby


ANSI escape codes start with ESC or character 33 Color codes (a subset of them), are 33[Xm where X is a semicolon-separated list of digits, possibly empty (this means a reset). m is a literal m.

Since I keep forgetting these codes myself I “documented” them on

As for a sed expression: I’d go for s/x1B[[0-9;]*[JKmsu]//g

Answered By: Dennis Kaarsemaker

You can do it in Perl.

#!/usr/bin/env perl

# catch the output of some command
my $output = `command`;

# remove only color codes
$output =~ s/e[[d;]*m//g;

# or remove all ANSI escape sequences
# $output =~ s/e[?.*?[@-~]//g

print "$output","n";
Answered By: Charles Boyd
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.