Question: I have some script that produces output with colors and I need to remove the ANSI codes.

```
#!/bin/bash

exec > >(tee log)   # redirect the output to a file but keep it on stdout
exec 2>&1

./somescript

```

The output is (in log file):

```
java (pid  12321) is running...@[60G[@[0;32m  OK  @[0;39m]

```

I didn't know how to put the ESC character here, so I put `@` in its place.

I changed the script into:

```
#!/bin/bash

exec > >(tee log)   # redirect the output to a file but keep it on stdout
exec 2>&1

./somescript | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

```

But now it gives me (in log file):

```
java (pid  12321) is running...@[60G[  OK  ]

```

How can I also remove this '`@[60G`?

Maybe there is a way to completely disable coloring for the entire script?

Answer: 
I couldn't get decent results from any of the other answers, but the following worked for me:

```
somescript | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g"

```

If I only removed the control char "^[", it left the rest of the color data, e.g., "33m". Including the color code and "m" did the trick. I'm puzzled with s/\x1B//g doesn't work because \x1B[31m certainly works with echo.
There's also a dedicated tool to handle ANSI escape sequences: [ansifilter](http://www.andre-simon.de/doku/ansifilter/en/ansifilter.html). Use the default `--text` output format to strip all ANSI escape sequences (note: not just coloring).

ref: <https://stackoverflow.com/a/6534712>