How to “un-ANSI-fy” logs

ansiconsoleshell

Lately there has been a tendency by command line tools to use fancy ANSI colors in their log output (for example, NPM and a host of nodejs based tools, rvm, docker, and a few other "modern" tools).

This makes it nice to read on the screen(*), but not so nice when you want to pipe the output to a log file or through a transport that does not handle ANSI well, like various remote execution tools and web based monitoring solutions. A lot of more mature tools at least had the decency to detect when the output is not a TTY and disable color, but not with those new fangled "user friendly" CLIs – which leaves me the try to get rid of the ANSI colors by piping the output through yet another process.

Sed-ing the output through s/\x1B\[[0-9]*\w//g seems to get the job done, but is there a cleaner approach then just copy pasting regular expressions into all of my scripts?

(*) at least until the the tool tries to display errors in dark red on your dark background terminal.

Best Answer

"ansifilter -p" might be useful to you...

It's in the Fedora repos; -p specifies plain output:

Name        : ansifilter
From repo   : updates
Summary     : ANSI terminal escape code converter
URL         : http://www.andre-simon.de/doku/ansifilter/ansifilter.php
License     : GPLv3+
Description : Ansifilter handles text files containing ANSI terminal escape codes.
            : The command sequences may be stripped or be interpreted to generate formatted
            : output (HTML, RTF, TeX, LaTeX, BBCode).