使用 Linux shell 脚本从文件中删除 ^H 和 ^M 字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6520009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove ^H and ^M characters from a file using Linux shell scripting
提问by Hasan
How do I remove ^H and ^M characters from a file using Linux shell scripting?
如何使用 Linux shell 脚本从文件中删除 ^H 和 ^M 字符?
^[[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
回答by Sergey
回答by Patrick J. S.
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
回答by sarnold
Two methods come to mind immediately:
立即想到两种方法:
tr -d control+vcontrol+h
sed 's/control+vcontrol+h//g'
tr -d control+vcontrol+h
sed 's/control+vcontrol+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
回答by subbu
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.
它只是删除每一行的最后一个字符,而不管字符是什么。这解决了我的问题。
回答by kenorb
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.
要排除其中一些(如行尾),请检查:从文件中删除控制字符。
回答by xiaweiss
if you want to change original file, do this:
如果要更改原始文件,请执行以下操作:
sed -i '.bak' 's/^M//g ; s/^H//g' test.md
(^M is control+v
control+m
)
(^H is control+v
control+h
)
(^M 是control+v
control+m
)
(^H 是control+v
control+h
)
much file, you can do this:
很多文件,你可以这样做:
find source -name '*.md' | xargs sed -i '.bak' 's/^M//g ; s/^H//g'