10

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.

Guss
  • 2,520
  • 5
  • 32
  • 55
  • 2
    See: http://unix.stackexchange.com/questions/14684/removing-control-chars-including-console-codes-colours-from-script-output - http://unix.stackexchange.com/questions/111899/how-to-strip-color-codes-out-of-stdout-and-pipe-to-file-and-stdout http://stackoverflow.com/questions/17998978/removing-colors-from-output http://superuser.com/questions/380772/removing-ansi-color-codes-from-text-stream -- Between 4+ questions over a span of 3 years across 3 sites we have only gotten answers to use a tool that does regex replacement/removal. I would guess there isn't anything better. – Zoredache Jan 14 '15 at 18:44
  • About the only thing that seems up for debate is which regex is the best for which situation. – Zoredache Jan 14 '15 at 18:45
  • 1
    `is there a cleaner approach then just copy pasting regular expressions into all of my scripts?` - Save your cleanup filter into a file called '/usr/local/bin/stripttycolor.sh` or something? That way you have the regex confined to a single location? – Zoredache Jan 14 '15 at 18:47
  • 3
    If you know that you will be writing the output to a file, the easiest thing to do (at least for NPM) is to run with the `--no-color` flag. Many other commands also have similar flags. – Moshe Katz Jan 15 '15 at 23:44
  • @MosheKatz: unfortunately not all commands do, for example `bower`. – Guss Jan 18 '15 at 13:34
  • @Zoredache: this is more of a problem with scripts in configurations, like build scripts in a CI, or setup scripts in a source repository - I don't want to rely on non-standard functionality because then installing the additional script is another system setup step that I'd need to manage (or just reduce complexity by having an implementation in each module instead of each use, still not really The Right Thing). – Guss Jan 18 '15 at 13:37
  • @Guss what version of Bower are you using? It has supported that flag [since 2012](https://github.com/bower/bower/commit/77fb081b9304064ceac244fa1eeab681ecc392d3). – Moshe Katz Jan 18 '15 at 14:03
  • Right. I couldn't find it in the documentation, so I assumed it doesn't work. silly me. – Guss Jan 18 '15 at 14:47

1 Answers1

1

"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).
Liczyrzepa
  • 455
  • 4
  • 13