每行带有 ^M 的文本文件 [英] Text file with ^M on each line

查看:27
本文介绍了每行带有 ^M 的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从朋友那里得到了一个源代码文件.该文件是在 UNIX 中创建的.当我使用 NotePad++ 在 Windows 中打开它时,每一行都有一个额外的空行.

不解,我下载了 Vim 并用它打开文件.然后我在每一行的末尾看到一堆 ^M.

这是什么^M?你如何防止它被插入?

解决方案

那些是 DOS/Windows 风格的行尾(迂腐,这就是他们现在通常所说的,但大多数早期的非 UNIX 操作系统,如 CP/M 和 OS/2 也有它们).在各种 Unices 上,行尾是 \n.在 DOS/Windows 中,行结束符是 \r\n(CR+LF 或 Carriage-Return and Line-Feed).\r 显示为 ^M.要在 vim 中删除它们,我会这样做:

:%s/^M//

您可以通过执行 CTRL+V 然后 CTRL+M

如果您使用的是 UNIX 系统,则可以使用 dos2unix.

I just got a source code file from a friend. The file was created in UNIX. When I opened it in Windows using NotePad++, each line had one extra blank line.

Puzzled, I downloaded Vim and used it to open the file. I then saw a bunch of ^M at the end of each line.

What is this ^M? How do you prevent it from being inserted?

解决方案

Those are DOS/Windows-style line-endings (to be pedantic that's what they're commonly known as now but most early non-UNIX OSes like CP/M and OS/2 had them as well). On the various Unices, line-endings are \n. In DOS/Windows the line-endings are \r\n (CR+LF or Carriage-Return and Line-Feed). The \r is what shows up as ^M. To remove them in vim, I do:

:%s/^M//

You can get the ^M by doing CTRL+V and then CTRL+M

If you are on a UNIX system, you can use dos2unix.

这篇关于每行带有 ^M 的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆