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
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 '14 at 16:52

5 Answers 5

up vote 35 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'


$ 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

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


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
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 '14 at 11:15

Remove color codes (special characters) with sed

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


Strip ANSI escape sequences in Python

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

#!/usr/bin/env python
import colorama, fileinput, sys;

for line in fileinput.input():

Run chmod +x stripcolorcodes.

share|improve this answer

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
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
Meh, if you set up cpanm correctly, it installs the module in about 5 seconds. –  Ryan Thompson Dec 4 '10 at 9:30

This sed command did it for me:

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


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

If your system has access to NodeJS you could install the following Node package, strip-ansi.

$ npm install -g strip-ansi

You can then run your command like so:

$ command-that-produces-colored-output | strip-ansi > outfile
share|improve this answer
Hi and welcome to the site. This answer was automatically flagged as "Low quality" because of it's length. In general, we like longer answers here that explain what the tools you suggest do. –  terdon Sep 16 '14 at 13:21

Your Answer


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.