3

I have text files that include the ANSI movement codes ESC[xC and ESC[xD. I want to filter these out but with each line output with those codes taken into account.

Consider the line:

this cat is greenESC[12DdogESC[4Cwhite

I would like this piped out as

this dog is white

Above ESC means the escape code \0x1b (or \033).

Scott
  • 33

1 Answers1

0

I'm sure there's a better way (and I'm sure there's better Perl), but this seems to do the trick:

perl -M5';$e="\x1b";' -lne 'chomp;if(/$e\[\d+[CD]/){$ns="";$p=0;while(/$e\[(\d+)([CD])/g){if(!$ns){$ns=$`;$p=length($ns)}$p+=($2eq"C"?+$1:-$1);($a=$'"'"')=~s/(^[^$e]+).*/$1/;if($a=~/^[^$e]/){substr($ns,$p,length($a),$a);$p+=length($a)}}print $ns}else{print $_}'
Scott
  • 33