How do I remove ^H and ^M characters from a file using Linux shell scripting?
如何删除^ H和M ^字符从一个文件中使用Linux shell脚本?
^[[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
5 个解决方案
#1
22
What you're seeing there are control characters, you simply could delete them with tr
你看到的是控制字符,你可以用tr删除它们!
cat your_file |
tr -d '\b\r'
this is better:
这是更好的:
tr -d '\b\r' < your_file
#2
6
Two methods come to mind immediately:
两种方法立刻浮现在脑海中:
tr -d control+v control+h
- tr - d控制+ v + h
sed 's/control+v control+h//g'
- sed ' s /控制+ v + 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
#3
2
For removing ^M characters appearing at the end of every line, I usually do this in vi editor.
删除M ^字符出现在每一行的末尾,我通常在vi编辑器。
:%s/.$//g
It just removes the last character of every line irrespective of what the character is. This solved my provlem.
它只是去掉了每一行的最后一个字符,不管字符是什么。这解决了我的provlem。
#4
0
Use sed
utility. See below as per examples:
使用sed效用。以下是例子:
sed 's/%//' file > newfile
echo "82%%%" | sed 's/%*$//'
echo "68%" | sed "s/%$//" #assume % is always at the end.
#5
0
You can remove all control characters by using tr
, e.g.
你可以使用tr来删除所有的控制字符。
tr -d "[:cntrl:]" file.txt
To exclude some of them (like line endings), check: Removing control characters from a file.
要排除其中一些(比如行尾),检查:从文件中删除控制字符。
#1
22
What you're seeing there are control characters, you simply could delete them with tr
你看到的是控制字符,你可以用tr删除它们!
cat your_file |
tr -d '\b\r'
this is better:
这是更好的:
tr -d '\b\r' < your_file
#2
6
Two methods come to mind immediately:
两种方法立刻浮现在脑海中:
tr -d control+v control+h
- tr - d控制+ v + h
sed 's/control+v control+h//g'
- sed ' s /控制+ v + 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
#3
2
For removing ^M characters appearing at the end of every line, I usually do this in vi editor.
删除M ^字符出现在每一行的末尾,我通常在vi编辑器。
:%s/.$//g
It just removes the last character of every line irrespective of what the character is. This solved my provlem.
它只是去掉了每一行的最后一个字符,不管字符是什么。这解决了我的provlem。
#4
0
Use sed
utility. See below as per examples:
使用sed效用。以下是例子:
sed 's/%//' file > newfile
echo "82%%%" | sed 's/%*$//'
echo "68%" | sed "s/%$//" #assume % is always at the end.
#5
0
You can remove all control characters by using tr
, e.g.
你可以使用tr来删除所有的控制字符。
tr -d "[:cntrl:]" file.txt
To exclude some of them (like line endings), check: Removing control characters from a file.
要排除其中一些(比如行尾),检查:从文件中删除控制字符。