Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

marked as duplicate by Gilles, jasonwryan, manatwork, Renan, Caleb Nov 13 '12 at 12:28

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.

add comment

2 Answers 2

up vote 4 down vote accepted

ANSI escape codes start with ESC or character \033 Color codes (a subset of them), are \033[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 https://github.com/seveas/hacks/blob/master/ansi.py.

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

share|improve this answer
    
A more complete list of ansi codes: en.wikipedia.org/wiki/ANSI_escape_code –  Dennis Kaarsemaker Nov 12 '12 at 23:14
add comment

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";
share|improve this answer
add comment

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