[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to remove escape sequences from a existing file



> I have a log file with escape sequences like "ESC]00m" and the like. I
> know they are the codes to change the color and so in the original
> printing, but in automatic post processing its a real headache...
> 
> How can I automatically remove ANY escape sequence to convert it into a
> real plain text?
> 

I think the only things you will have to convert are the ESC strings
themselves (the ]00m can be left literal). This perl script will
convert all the ESC's back into their actual binary values (ascii 27):

====================

#!/usr/bin/perl

foreach (<>){
  s/ESC/chr(27)/eg;
  print;
}

====================

Just put it in a file named 'conv.pl' , make it executable, and run it like:

conv.pl < logfile > converted_logfile

or

conv.pl logfile > converted_logfile

The limitations of this script is that it will convert *any* literal
string of ESC to a non-pritable character (for example in
DESCRIPTION). This could be fixed by modifying the regular expression
if we knew more context (does ] ALWAYS follow ESC in the escape
sequences? Do they always consist of 5 characters? What letters can
they end in?)

For the most past, this should do the trick though.



Reply to: