Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove ^H and ^M characters from a file using Linux shell scripting

Tags:

linux

shell

How do I remove ^H and ^M characters from a file using Linux shell scripting?

^[[0^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H   rcv-packets: 0
^[[0^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H      rcv-errs: 0
     rcv-drop: 0
     rcv-fifo: 0
     rcv-frame: 0
like image 445
Hasan Avatar asked Jun 29 '11 11:06

Hasan


People also ask

How do I find Ctrl M characters in Linux?

Note: Remember how to type control M characters in UNIX, just hold the control key and then press v and m to get the control-m character.

How do I remove special characters from VI?

To escape a special character, precede it with a backslash ( \ ). For example, to search for the string “anything?” type /anything\? and press Return. You can use these special characters as commands to the search function.


2 Answers

What you're seeing there are control characters, you simply could delete them with tr

cat your_file |
tr -d '\b\r'

this is better:

tr -d '\b\r' < your_file
like image 169
Patrick J. S. Avatar answered Oct 05 '22 23:10

Patrick J. S.


Two methods come to mind immediately:

  • tr -d control+v control+h
  • sed 's/control+v control+h//g'

Here's both in action:

$ od -c test
0000000  \b   h   e   l   l   o  \b   t   h   e   r   e  \b  \n
0000016
$ sed 's/^H//g' < test | od -c
0000000   h   e   l   l   o   t   h   e   r   e  \n
0000013
$ tr -d ^H < test | od -c
0000000   h   e   l   l   o   t   h   e   r   e  \n
0000013
like image 27
sarnold Avatar answered Oct 05 '22 22:10

sarnold