[ACCEPTED]-Remove ^H and ^M characters from a file using Linux shell scripting-shell

Accepted answer
Score: 24

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

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

this is better:

tr -d '\b\r' < your_file
Score: 8

Two methods come to mind immediately:

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

Here's 1 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
Score: 2

For removing ^M characters appearing at 4 the end of every line, I usually do this 3 in vi editor.

:%s/.$//g

It just removes the last character 2 of every line irrespective of what the character 1 is. This solved my provlem.

Score: 0

Use sed utility. See below as per examples:

sed 's/%//' file > newfile
echo "82%%%" | sed 's/%*$//'
echo "68%" | sed "s/%$//" #assume % is always at the end.

0

Score: 0

You can remove all control characters by 2 using tr, e.g.

tr -d "[:cntrl:]" file.txt

To exclude some of them (like 1 line endings), check: Removing control characters from a file.

Score: 0

if you want to change original file, do 2 this:

sed -i '.bak' 's/^M//g ; s/^H//g' test.md

(^M is control+v control+m)
(^H is control+v control+h)

much file, you can 1 do this:

find source -name '*.md' | xargs sed -i '.bak' 's/^M//g ; s/^H//g'

More Related questions