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.

I have a command that produces output in color, and I would like to pipe it into a file with the color codes stripped out. Is there a command that works like cat except that it strips color codes? I plan to do something like this:

$ command-that-produces-colored-output | stripcolorcodes > outfile
share|improve this question
3  
It might be a bug that a program produces colored output even then it's output is redirected to a file and doesn't provide an option to switch it off. I'd expect any program that colors it's output to check isatty(stdin) before doing so. Mind to share what is that program? –  alex Dec 4 '10 at 6:44
    
Why not | less -r > ? –  Sergei May 14 at 16:52
add comment

5 Answers

up vote 31 down vote accepted

You'd think there'd be a utility for that, but I couldn't find it. However, this Perl one-liner should do the trick:

perl -pe 's/\e\[?.*?[\@-~]//g'

Example:

$ command-that-produces-colored-output | perl -pe 's/\e\[?.*?[\@-~]//g' > outfile

Or, if you want a script you can save as stripcolorcodes:

#! /usr/bin/perl

use strict;
use warnings;

while (<>) {
  s/\e\[?.*?[\@-~]//g; # Strip ANSI escape codes
  print;
}

If you want to strip only color codes, and leave any other ANSI codes (like cursor movement) alone, use

s/\e\[[\d;]*m//g;

instead of the substitution I used above (which removes all ANSI escape codes).

share|improve this answer
    
Accepting this answer because it is a no-install solution. –  Ryan Thompson Dec 4 '10 at 9:31
1  
As mentioned in a comment to another answer (unix.stackexchange.com/questions/4527/…), this regex was pulled from Term::ANSIColor. Acknowledge your sources, please! –  forivall Nov 22 '13 at 0:13
    
acknowledge your sources! –  Pineapple Under the Sea May 21 at 11:15
add comment

Remove color codes (special characters) with sed

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

Or

Strip ANSI escape sequences in Python

Install colorama python package (pip install colorama). Put into stripcolorcodes:

#!/usr/bin/env python
import colorama, fileinput, sys;
colorama.init(strip=True);

for line in fileinput.input():
    sys.stdout.write(line)

Run chmod +x stripcolorcodes.

share|improve this answer
add comment

If you can install the Term::ANSIColor module, this perl script works:

#!/usr/bin/env perl
use Term::ANSIColor qw(colorstrip);
print colorstrip $_ while <>;
share|improve this answer
3  
That's actually where I got that color-codes-only regex. But installing a whole module just for that one regex is a bit much. –  cjm Dec 4 '10 at 2:00
1  
Meh, if you set up cpanm correctly, it installs the module in about 5 seconds. –  Ryan Thompson Dec 4 '10 at 9:30
add comment

This sed command did it for me:

sed -r "s/\\^\\[(\\[[^@-~]+[@-~]|[0-9@-_]|%@)//g"

Example:

$ command-that-produces-colored-output | sed -r "s/\\^\\[(\\[[^@-~]+[@-~]|[0-9@-_]|%@)//g" > outfile
share|improve this answer
add comment

You can simply use | less -r >

share|improve this answer
    
That doesn't appear to do anything. The output still contains color codes. –  Ryan Thompson May 14 at 18:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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