Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

What command can I use to strip color-code escape sequences from a text file? Ideally something I can pipe through. If I have a file with a bunch of coloured text rainbow.txt, what goes in the gap:

cat rainbox.txt | *something* > plain.txt

I'm working in bash on CentOS 4.4.

share|improve this question

4 Answers 4

up vote 25 down vote accepted

Try:

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
share|improve this answer
    
Perfect, thank you. –  kdt Oct 5 '09 at 9:27
    
Of the 3 answers this was the only one that worked. –  slm Jul 18 at 22:41

cat rainbox.txt | col -b > plain.txt

share|improve this answer
    
Why isn't there any upvote on this? The only solution that works for me! And it's damn simple too! –  phunehehe Dec 4 '11 at 15:35
1  
Because it does not work for me. –  lzap Mar 20 '12 at 10:04
4  
This only gets rid of the escape characters, not their arguments. –  Aaron Sep 7 '12 at 13:22

You can't, because what is an escape sequence isn't well-defined in general -- you need to know what sort of terminal your escape sequences are designed for. If you want to restrict the problem to "strip ANSI colour sequences" (a fairly likely assumption), something like:

sed 's/\o033\[[0-9]*;[0-9]*m//g'

Should do the trick.

share|improve this answer

The following will capture the [Xm, [X;m, [X;Ym, and [X;Y;Zm possibilities (some of those may be technically incorrect, but they work and have been seen in the wild):

sed -r 's|\x1B\[[0-9]{1,2};?(;[0-9]{1,2}){,2}m||g'
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.