Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Examining the output from

perl -e 'use Term::ANSIColor; print color "white"; print "ABC\n"; print color "reset";'

in a text editor (e.g., vi) shows the following:

^[[37mABC
^[[0m

How would one remove the ANSI color codes from the output file? I suppose the best way would be to pipe the output through a stream editor of sorts.

The following does not work

perl -e 'use Term::ANSIColor; print color "white"; print "ABC\n"; print color "reset";' | perl -pe 's/\^\[\[37m//g' | perl -pe 's/\^\[\[0m//g'
share|improve this question
    
Not an answer to the question, but you can also pipe the output to more or less -R which can interpret the escape codes as color instead of a text editor. – terdon Jul 3 '13 at 13:50
up vote 24 down vote accepted

Using sed:

perl -e 'use Term::ANSIColor; print color "white"; print "ABC\n"; print color "reset";' | 
sed 's/\x1b\[[0-9;]*m//g'
  • \x1b is the escape special character
  • \[ is the second character of color statement
  • [0-9]* is the color value
  • m is the last character of color statement

sed is enough, but you can still use perl instead of sed:

perl -e 'use Term::ANSIColor; print color "white"; print "ABC\n"; print color "reset";' | 
perl -pe 's/\x1b\[[0-9;]*m//g'
share|improve this answer
    
Wonderful, thank you so much. – user001 Jan 21 '12 at 1:56
1  
Thanks for the sed command and the explanation. :) – Redsandro Feb 5 '13 at 14:15
1  
Some color codes (e.g. Linux terminal) contain a prefix, e.g. 1;31m so better add ; to your regex: cat colored.log | sed -r 's/\x1b\[[0-9;]*m//g' or they won't be stripped. – Redsandro Mar 3 '14 at 13:11

I have found out a better escape sequence remover. Check this:

perl -pe 's/\x1b\[[0-9;]*[mG]//g'

share|improve this answer

What is displayed as ^[ is not ^ and [; it is the ASCII ESC character, produced by Esc or Ctrl[ (the ^ notation means the Ctrl key).

ESC is 0x1B hexadecimal or 033 octal, so you have to use \x1B or \033 in your regexes:

perl -pe 's/\033\[37m//g; s/\033[0m//g'

perl -pe 's/\033\[\d*(;\d*)*m//g'
share|improve this answer
    
Wonderful, thank you so much. – user001 Jan 21 '12 at 1:56

The "answered" question didn't work for me, so I created this regex instead to remove the escape sequences produced by the perl Term::ANSIColor module.

cat colors.o | perl -pe 's/\x1b\[[^m]+m//g;

Grawity's regex should work fine, but using +'s appears to work ok too.

share|improve this answer
    
(1) What do you mean by The "answered" question?  Do you mean the accepted answer?  (2) This command does not work — it does not even execute — because it has an unmatched (unbalanced) quote.  (3) This a useless use of cat (UUOC) — it should be possible to do perl -pe command colors.o.  (4) Who ever said anything about the codes being in a .o file? – Scott Feb 11 at 5:35

If you prefer something simple you could use the strip-ansi module (Node.js required):

$ npm install --global strip-ansi-cli

Then use it like this:

$ strip-ansi < colors.o

Or just pass in a string:

$ strip-ansi '^[[37mABC^[[0m'
share|improve this answer
    
This a useless use of cat (UUOC) — it should be possible to do strip-ansi colors.o or at least strip-ansi < colors.o. – Scott Feb 11 at 5:36
    
@Scott Sure, you can also do strip-ansi < colors.o, but from experience people are more familiar with piping. I've updated the answer. – Sindre Sorhus Feb 11 at 9:09

I had similar problem with removing characters added from collecting interactive top output via putty and this helped:

cat putty1.log | perl -pe 's/\x1b.*?[mGKH]//g'
share|improve this answer
    
This a useless use of cat (UUOC) — it should be possible to do perl -pe command putty1.log. – Scott Feb 11 at 5:36

Your Answer

 
discard

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.