17

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.

kdt
  • 1,400
  • 3
  • 22
  • 34

4 Answers4

30

Try:

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
rkthkr
  • 8,618
  • 28
  • 38
4

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

Vi.
  • 841
  • 11
  • 19
1

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'
linux_sa
  • 111
  • 1
1

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.

womble
  • 96,255
  • 29
  • 175
  • 230