Unix 换行符到 Windows 换行符(在 Windows 上)
Windows 中是否有一种方法(例如 PowerShell 或工具)可以递归目录并将任何 Unix 文件转换为 Windows 文件。
我对 PowerShell 中至少检测 Unix 文件的方法非常满意。
对一个文件执行此操作很容易,但我想要一些更具可扩展性的东西(因此倾向于 PowerShellish 解决方案)。
Is there a way (say PowerShell, or a tool) in Windows that can recurse over a directory and convert any Unix files to Windows files.
I'd be perfectly happy with a way in PowerShell to at least detect a Unix file.
It's easy do this for one single file, but I'm after something a bit more scalable (hence leaning towards a PowerShellish solution).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您有兴趣,这里是纯 PowerShell 方式。
查找至少具有一个 Unix 行结尾的文件 (PowerShell v1):
以下是查找 Unix 行结尾并将其隐藏为 Windows 行结尾的方法。 需要注意的一件重要事情是,如果文件末尾还没有结束行,则会在文件末尾添加一个额外的行结束符 (\r\n)。 如果您确实不想这样做,我将发布一个示例来说明如何避免它(它有点复杂)。
上面的方法之所以有效,是因为 PowerShell 会自动分割 \n 上的内容(如果存在则删除 \r),然后在将每个内容(在本例中为一行)写入文件时添加 \r\n。 这就是为什么您总是以文件末尾结尾的行结束。
另外,我编写了上面的代码,以便它只修改它需要的文件。 如果您不关心这一点,可以删除
if
语句。 哦,请确保只有文件到达 ForEach -对象。 除此之外,您可以在管道开始时执行任何您想要的过滤。Here is the pure PowerShell way if you are interested.
Finding files with at least one Unix line ending (PowerShell v1):
Here is how you find and covert Unix line endings to Windows line endings. One important thing to note is that an extra line ending (\r\n) will be added to the end of the file if there isn't already a line ending at the end. If you really don't want that, I'll post an example of how you can avoid it (it is a bit more complex).
The above works because PowerShell will automatically split the contents on \n (dropping \r if they exist) and then add \r\n when it writes each thing (in this case a line) to the file. That is why you always end up with a line ending at the end of the file.
Also, I wrote the above code so that it only modifies files that it needs to. If you don't care about that you can remove the
if
statement. Oh, make sure that only files get to the ForEach-Object. Other than that, you can do whatever filtering you want at the start of that pipeline.这似乎对我有用。
This seems to work for me.
Cygwin 中有 dos2unix 和 unix2dos。
There is dos2unix and unix2dos in Cygwin.
下载 Vim,打开文件,并对
多个文件(全部为 *.txt C:\tmp 中的文件 - 递归):
Download Vim, open your file, and issue
Batch for multiple files (all *.txt files in C:\tmp - recursive):
您可以使用 Visual Studio。 菜单文件→高级保存选项...。
You can use Visual Studio. Menu File → Advanced Save Options....
转换为 Windows 文本可能很简单:
使用以下命令(带有负向后查找)。 如果没有
-nonewline
,set-content 会在底部添加一个额外的 `r`n。 使用括号,您可以修改同一个文件。 这对于意外地对同一个文件执行两次操作应该是安全的。相反,Windows 到 Unix 文本:
这是另一个版本,用于处理无法放入内存的大文件。 但输出文件必须不同。
示例(输入和输出文件可以相同):
如果您有 Emacs,则可以使用 esc-x hexl-mode 进行检查。 记事本无法正确显示 Unix 文本; 一切都将在同一条线上。 我必须指定
set-content
的路径,因为-replace
会删除pspath
属性。Converting to Windows text could be as simple as:
Use the following (with negative lookbehind). Without
-nonewline
, set-content puts an extra `r`n at the bottom. With the parentheses, you can modify the same file. This should be safe on doing to the same file twice accidentally.The reverse would be this, Windows to Unix text:
Here's another version for use with huge files that can't fit in memory. But the output file has to be different.
Examples (input and output file can be the same):
If you have Emacs, you can check it with
esc-x hexl-mode
. Notepad won't display Unix text correctly; it will all be on the same line. I have to specify the path forset-content
, because-replace
erases thepspath
property.如果 Cygwin 不适合您,如果您通过 google 搜索,可以找到许多 Windows 下 unix2dos 的独立可执行文件,或者您也可以自己编写一个。 请参阅我的类似(转换方向相反)问题 此处。
If Cygwin isn't for you, there are numerous stand-alone executables for unix2dos under Windows if you google around, or you could write one yourself. See my similar (opposite direction for conversion) question here.
在 10,000 个文件的循环中测试先前答案中给出的代码的结果,其中许多文件的大小超过 50 KB:
底线是 PowerShell 代码对于大文件和大量文件来说非常低效/缓慢/无法使用文件。 它也不保留 BOM 字节。 我发现 unix2dos 7.2.3 是最快、最实用的解决方案。
The result of testing the code given in a previous answer in a loop with 10,000 files, many of them more than 50 KB in size:
The bottom line is the PowerShell code is very inefficient/slow/unusable for large files and large number of files. It also does not preserve BOM bytes. I found unix2dos 7.2.3 to be the fastest and most practical solution.
这个对我有用:
It works for me:
基于js2010的答案我创建了这个脚本:
Building on js2010's answer I've created this script:
在 WordPad 中打开带有 Unix 行结尾的文件并保存它会将所有行结尾重写为 DOS 。 对于大量文件来说,这有点费力,但对于偶尔的几个文件来说,它已经足够好了。
Opening a file with Unix line endings in WordPad and saving it will rewrite all the line endings as DOS. It is a bit laborious for large numbers of files, but it works well enough for a few files every once in a while.