[Nottingham] Stripping ANSI colour sequences from a log file

Michael Quaintance penfoldq at penfoldq.co.uk
Tue Mar 29 15:05:59 UTC 2011


Hi all

I have a number of log files with embedded ANSI colour codes which 
render fine on a terminal but are somewhat annoying when trying to 
process the resulting files using scripts.

I know there are multiple ways to fix this. I could turn off colour 
support in my termcap and/or tell the programs I'm logging not to use 
colour, but I'd like to keep the colour intact for when I am watching it 
("tail -f" or interactively when the log is a result of a "script" 
session). I would like, however to be able to run the resulting log 
file(s) through something that will strip the ANSI colour sequences and 
leave the rest alone (along the lines of dos2unix).

I could use some kind of sed-fu:

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

but I was hoping to get an idea of what others do for this. My googling 
so far has only produced examples of everyone rolling their own which is 
possible but then everyone makes their own mistakes and I would prefer 
something that had been reasonably tested and I did not have to worry 
about.

Anyone already done this?

One of the suggestions I saw was "col -b" but that does not seem to 
understand ANSI colour sequences and only removes the escape character 
and the following '[' but leaves the actual colour code (e.g. "00;34m") 
in the output.

Thanks in advance.

-Penfold



More information about the Nottingham mailing list