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
  • 4
    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
    Commented Dec 4, 2010 at 6:44
  • pass it through cat - quick test I ran grep --color=auto myusername /etc/passwd gives me my username in red with white text elsewhree. grep --color=auto myusername /etc/passwd | cat gives me plain white text
    – ivanivan
    Commented Nov 15, 2017 at 2:48
  • 3
    That's not a solution. It only works because grep --color=auto avoids producing colored output when standard output is not a terminal. I'm talking about a command that unconditionally outputs color codes. (As alex points out above, such behavior is arguably a bug, but sometimes we need to work with imperfect software that we can't easily fix ourselves, and that's what this question is about.) Commented Nov 15, 2017 at 4:00
  • Possibly relevant unix.stackexchange.com/questions/14684/… Commented Jun 10, 2018 at 14:22
  • Reinforcing Ryan's post: grep --colour=always myusername /etc/passwd | cat keeps the username in red.
    – user516667
    Commented Aug 25, 2022 at 8:36

7 Answers 7


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


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

  • 2
    Didn't try python, but the sed version doesn't strip all colors. (I didn't test exhaustively, but it left some background colors intact, so I went with the faster perl option.)
    – rsaw
    Commented Jan 17, 2018 at 23:48

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

#!/usr/bin/env perl
use Term::ANSIColor qw(colorstrip);
print colorstrip $_ while <>;
  • 8
    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
    Commented Dec 4, 2010 at 2:00
  • 5
    Meh, if you set up cpanm correctly, it installs the module in about 5 seconds. Commented Dec 4, 2010 at 9:30
  • 1
    There is no need to write the script to the file. Just pipe into: perl -mTerm::ANSIColor=colorstrip -n -e 'print colorstrip $_' Commented Jun 25, 2019 at 14:07

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
  • 3
    NodeJS is a huge dependency, when there are solutions that can use languages and tools already present in most systems.
    – user137369
    Commented Nov 14, 2017 at 16:50
  • Also, this is the slowest solution listed (see here) and you probably will notice the difference.
    – user254560
    Commented Nov 17, 2017 at 8:23
  • It's worth noting that all this module does is replaces all matches of the regex string.replace(ansiRegex(), '') which is defined here github.com/chalk/ansi-regex/blob/main/index.js.
    – Andy
    Commented Jun 13, 2021 at 2:23
$ command-that-produces-colored-output | ansifilter

... and if necessary, (dnf, ...) install ansifilter


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
  • This doesn't seem to work with the latest version of sed.
    – user254560
    Commented Nov 17, 2017 at 8:22

You can use ac for this. It should be faster than anything listed above (though perl and sed should be decent). Here's an example:

curl -s wttr.in/LA | ac -s

As a disclaimer: the color filtering functionality was written by me.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .