Linux Shell命令从文本文件中去除^M个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13589895/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:53:39  来源:igfitidea点击:

Shell command to strip out ^M characters from text file

linuxsedawkgrep

提问by Homunculus Reticulli

Possible Duplicate:
Remove carriage return in Unix

可能的重复:
在 Unix 中删除回车

I am reading some data generated by an external third party. I have noticed that the ASCII text in the file is interspersed with ^M characters, which I believe is character 13 in ASCII and represents a carriage return without linefeed.

我正在读取外部第三方生成的一些数据。我注意到文件中的 ASCII 文本散布着 ^M 字符,我认为这是 ASCII 中的字符 13,表示没有换行符的回车。

Is there a one liner I can use to strip the ^M characters from the file?

有没有一种衬里可以用来从文件中去除 ^M 字符?

I am running on Linux (Ubuntu).

我在 Linux (Ubuntu) 上运行。

采纳答案by anubhava

You can use sed like this:

您可以像这样使用 sed:

sed -i.bak 's/^M$//' infile.txt

To type ^M, you need to type CTRL-Vand then CTRL-M.

要键入^M,您需要键入CTRL-V然后CTRL-M

回答by shellter

OR

或者

dos2unix infile.txt file2.txt ....

OR

或者

man dos2unix 

for more details.

更多细节。