Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

6 Answers 6

up vote 15 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
    
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 at 13:11

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

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

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

share|improve this answer

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

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

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

$ npm install --global strip-ansi

Then use it like:

$ cat colors.o | strip-ansi
share|improve this answer

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.