使用CMD将文本文件中的所有CR转换为CRLF [英] Convert all CR to CRLF in text file using CMD

查看:518
本文介绍了使用CMD将文本文件中的所有CR转换为CRLF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将文本文件中的所有CR转换为CRLF?

Is there a way to convert all CRs to CRLFs in a text file?

当我从Windows上的Linux服务器打开一个文本文件时,所有文本都显示在一行中,但实际上是多行显示.

When I open a text file from Linux server on Windows, all text is displayed in one line, but actually it's a multi line one.

我想在一个批处理文件中执行转换.

I'd like to perform the conversion in a batch file.

有人可以请教吗?

推荐答案

只要存在不止一种系统和交换数据的冲动,行分隔符和行终止符就一直是系统之间兼容性冲突的根源. 换行符上的维基百科文章对历史背景进行了不错的概述.并且,它提出了针对该问题的各种解决方案,专门用于Unix端或Windows端.

Line separators and line terminators have been a source of compatibility friction between systems as long as there has been more than one kind of system and an urge to exchange data. The Wikipedia article on the Newline has a decent overview of the historical context. And, it suggests a variety of solutions to this problem specifically for use on the Unix side or the Windows side.

在Unix(Linux)端,查找名为unix2dos的实用程序及其近亲dos2unix.这些通常可以作为商业Unix的组件或作为开放源代码工具使用.如果有的话,它们是最好的答案,因为它们(通常,请参见您Verson的手册页以获取详细信息)对意外写有两个行尾的文件非常小心.在这种情况下,通过这两个实用程序进行的旅行通常会清理文件以使其内部保持一致.在没有这些方便的命令的情况下,可以使用许多本机实用程序进行转换.例如,可以使用tr命令将DOS CRLF行尾转换为Unix换行符:

On the Unix (Linux) side, look for a utility named unix2dos and its close relative dos2unix. These are commonly available, either as a component of a commercial Unix or as open source tools. If available, they are the best answer because they (usually, see your verson's man pages for details) are careful about files that are accidentally written with both line endings. In that unfortunate case, a trip through both utilities will usually clean up the file to be internally consistent. In the absence of these convenient commands, many native utilities can be made to do the conversion. For instance, converting DOS CRLF line endings to Unix newlines can be done with the tr command:

$ tr -d '\r' < inputfile > outputfile

但是请注意,该命令假定所有行均由CRLF(或LFCR)终止,并且只需从输入中删除每个CR字符即可工作.任何裸露的CR字符都将丢失.

But do note the caveat that this command assumed that all lines were terminated by CRLF (or LFCR) and works by simply deleting every CR character from the input. Any naked CR characters will be lost.

在DOS和Windows方面,它曾经是很多空白. unix2dosdos2unix端口确实存在,例如,它们包含在 much 大型Cygwin工具中,这些工具在Windows计算机上提供了完整的unix仿真.但是很难找到仅使用内置功能的解决方案.

On the DOS and Windows side, it used to be a lot bleaker. Ports of unix2dos and dos2unix certainly exist, for instance they are included in the much larger Cygwin tools that provide a complete unix emulation on a Windows machine. But a solution using only built-in features was hard to find.

现代Windows(可能是Windows XP之后)更好.在那里,内置的FIND命令比以前更容易选择行终止符,并且可以用来执行从Unix行尾到DOS尾端的所需转换.上面引用的Wiki页面给出了此配方:

Modern Windows (probably since Windows XP), however, is better. There, the built-in FIND command is much less touchy about choice of line terminator than it used to be, and can be used to do the required conversion from Unix line endings to DOS endings. The Wiki page cited above gives this recipe:

C:\...> TYPE filename.u | FIND "" /V >filename.txt

实验表明,这种方法同样有效,但是由于未知原因,它可能不会给出相同的结果:

Experimentation shows that this works as well, but it may not give identical results for unknown reasons:

C:\...> FIND "" /V <filename.u >filename.txt

在两种情况下,您都将创建一个具有更改的行尾的文件副本.可能不建议在适当位置更改文件.

In both cases, you create a copy of the file with the changed line endings. It would probably not be recommended to change the files in place.

我将提到另一种在纸上似乎总是很诱人的方法.当您使用Samba在Linux服务器上提供文件系统共享以供Windows挂载时,您可以为该共享设置一个配置选项,以文本模式"将其挂载.在文本模式"下安装的共享会自动转换行尾.如果它对您有用,那可能是最干净的解决方案.两种系统都使用其首选的文本文件格式,而不必大惊小怪.但是要仔细测试,此解决方案充满了边缘情况和陷阱.最重要的是,不要期望文本模式文件系统挂载点上的二进制文件能够正确读取.他们经常会,但不一定总是.

I'll mention one other approach that always seems tempting on paper. When you use Samba to provide the file system share on the Linux server for mounting by Windows, there is a configuration option you can set for the share that mounts it in "text mode". Shares mounted in "text mode" automatically have line endings converted. If it works for you, that is probably the cleanest possible solution. Both systems use their preferred text file format, and neither has to fuss about it. But test carefully, this solution is full of edge cases and pitfalls. Most importantly, don't expect binary files on a text mode file system mount point to read correctly. They often will, but not necessarily always.

这篇关于使用CMD将文本文件中的所有CR转换为CRLF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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