Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 38 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! – Michelle 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
This is awesome. Exactly what I needed! – adivis12 Apr 8 at 15:06

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

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

$ npm install -g strip-ansi-cli

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
this is the only one of the listed answers that works with grunt for me. – rbrc Jul 21 '15 at 3:21

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

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.