Search code examples
linuxterminalterminfo

How to remove terminal control escape sequences in the file?


I got a log from remote linux computer. It looks like:

2013-10-23T08:19:05+0300 Last login: Wed Oct 23 08:17:38 EEST 2013 from 10.9.167.55 on pts/0  
2013-10-23T08:19:05+0300 Last login: Wed Oct 23 08:19:05 2013 from 10.9.167.55^M  
2013-10-23T08:19:07+0300 ^[[?1034h-bash-4.1$ date  
2013-10-23T08:19:07+0300 Wed Oct 23 08:19:07 EEST 2013  
2013-10-23T08:19:08+0300 -bash-4.1$ ls  
2013-10-23T08:19:08+0300 ^[[0m^[[01;34m99^[[0m #avail.info ^[[01;34mgmoTemp^[[0m raml21.dtd SNMP4JTestAgentBC.cfg  
2013-10-23T08:19:08+0300 an_mainHost_localhost_20131023081654000136.xml #avail.info~ gsh.txt ^[[01;34mresults^[[0m  
2013-10-23T08:19:09+0300 ^[[m-bash-4.1$ exit  
2013-10-23T08:19:09+0300 logout

But it should be:

Last login: Wed Oct 23 08:17:38 EEST 2013 from 10.9.167.55 on pts/0  
Last login: Wed Oct 23 08:19:05 2013 from 10.9.167.55  
-bash-4.1$ date  
Wed Oct 23 08:19:07 EEST 2013  
-bash-4.1$ ls  
99                                              #avail.info   gmoTemp  raml21.dtd  SNMP4JTestAgentBC.cfg  
an_mainHost_localhost_20131023081654000136.xml  #avail.info~  gsh.txt  results  
-bash-4.1$ exit  
logout  

The messy codes are terminal control escape sequences, you can use command "infocmp xterm" and "man terminfo" to get more details.
My question is how can I remove these terminal control escape sequences in the file?
Thanks a lot!


Solution

  • I solved this issue using lots of regular expressions according to http://invisible-island.net/xterm/ctlseqs/ctlseqs.html