Program that passes STDIN to STDOUT with color codes stripped?

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).


Remove color codes (special characters) with GNU 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.


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

#!/usr/bin/env perl
use Term::ANSIColor qw(colorstrip);
print colorstrip $_ while <>;