用于转换 Unix 行尾的 Windows 命令?

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

Windows command to convert Unix line endings?

windowsunixbatch-filecmdeol

提问by Deepti Jain

Is there a Windows command to convert line endings of a file?

是否有 Windows 命令来转换文件的行尾?

We have a test.batwhich we need to run to start our server. We use Perforce and we need to have unix line endings in our workspace. For some reason, we are not allowed to change line endings to Windows in our workspaces. However, the server runs on Windows.

我们有一个test.bat我们需要运行它来启动我们的服务器。我们使用 Perforce,我们需要在我们的工作区中有 unix 行结尾。出于某种原因,我们不允许在我们的工作区中将行结尾更改为 Windows。但是,服务器在 Windows 上运行。

Everytime I have to run the bat file, I open it in Notepad++ and choose Edit→EOL conversion→Windows. Is there a way to automate this so that we won't need to manually change the line endings everytime we sync with Perforce?

每次必须运行 bat 文件时,我都会在 Notepad++ 中打开它,然后选择“编辑”→“EOL 转换”→“Windows”。有没有办法自动执行此操作,以便我们每次与 Perforce 同步时都不需要手动更改行尾?

Thanks in advance.

提前致谢。

回答by TampaHaze

This can actually be done very easily using the morecommand which is included in Windows NT and later. To convert input_filenamewhich contains UNIX EOL (End Of Line) \nto output_filenamewhich contains Windows EOL \r\n, just do this:

这实际上可以使用more包含在 Windows NT 和更高版本中的命令非常轻松地完成。要将input_filename包含 UNIX EOL (End Of Line) 的内容\n转换output_filename为包含 Windows EOL 的内容\r\n,只需执行以下操作:

TYPE input_filename | MORE /P > output_filename

The morecommand has additional formatting options that you may not be aware of. Run more/?to learn what else morecan do.

more命令具有您可能不知道的其他格式选项。运行more/?以了解还能more做什么。

回答by David Jashi

Use unix2dosutility. You can download binaries here.

使用unix2dos实用程序。您可以在此处下载二进制文件。

回答by Ansgar Wiechers

You can do this without additional tools in VBScript:

您无需在 VBScript 中使用其他工具即可完成此操作:

Do Until WScript.StdIn.AtEndOfStream
  WScript.StdOut.WriteLine WScript.StdIn.ReadLine
Loop

Put the above lines in a file unix2dos.vbsand run it like this:

将以上几行放在一个文件中unix2dos.vbs并像这样运行它:

cscript //NoLogo unix2dos.vbs <C:\path\to\input.txt >C:\path\to\output.txt

or like this:

或者像这样:

type C:\path\to\input.txt | cscript //NoLogo unix2dos.vbs >C:\path\to\output.txt


You can also do it in PowerShell:

您也可以在 PowerShell 中执行此操作:

(Get-Content "C:\path\to\input.txt") -replace "`n", "`r`n" |
  Set-Content "C:\path\to\output.txt"

which could be further simplified to this:

可以进一步简化为:

(Get-Content "C:\path\to\input.txt") | Set-Content "C:\path\to\output.txt"

The above statement works without an explicit replacement, because Get-Contentimplicitly splits input files at any kind of linebreak (CR, LF, and CR-LF), and Set-Contentjoins the input array with Windows linebreaks (CR-LF) before writing it to a file.

上述语句无需显式替换即可工作,因为Get-Content在任何类型的换行符(CR、LF 和 CR-LF)处隐式拆分输入文件,并Set-Content在将输入数组写入文件之前将其与 Windows 换行符 (CR-LF) 连接起来。

回答by Jurosh

I was dealing with CRLFissues so I decided to build really simple tool for conversion (in NodeJS):

我正在处理CRLF问题,所以我决定构建非常简单的转换工具(在 NodeJS 中):

It's NodeJS EOL converter CLI

它是NodeJS EOL 转换器 CLI

So if you have NodeJS with npminstalled you can try it:

因此,如果您安装了带有 npm 的 NodeJS,您可以尝试一下:

npm i -g eol-converter-cli
eolConverter crlf "**/*.{txt,js,java,etc}"

Path might be configured dynamically by using Glob regex (same regex as in shell).

可以使用 Glob 正则表达式(与 shell 中的正则表达式相同)动态配置路径。

So if you can use NodeJS, it's really simple and you can integrate this command to convert whole workspace to desired line endings.

因此,如果您可以使用 NodeJS,那真的很简单,您可以集成此命令将整个工作区转换为所需的行尾。

回答by kxr

Windows' MORE is not reliable, it destroys TABs inevitably and adds lines.

Windows 的 MORE 不可靠,它不可避免地会破坏 TAB 并添加行。

unix2dosis part also of MinGW/MSYS, Cygutils, GnuWin32 and other unix binary port collections - and may already be installed.

unix2dos也是 MinGW/MSYS、Cygutils、GnuWin32 和其他 unix 二进制端口集合的一部分 - 并且可能已经安装。

When pythonis there, this one-liner converts any line endings to current platform - on any platform:

python存在时,这个单行将任何行结尾转换为当前平台 - 在任何平台上:

TYPE UNIXFILE.EXT | python -c "import sys; sys.stdout.write(sys.stdin.read())" > MYPLATFILE.EXT

or

或者

python -c "import sys; sys.stdout.write(open(sys.argv[1]).read())" UNIXFILE.EXT > MYPLATFILE.EXT

Or put the one-liner into a .bat / shell script and on the PATH according to your platform:

或者根据您的平台将单行代码放入 .bat / shell 脚本和 PATH 中:

@REM This is any2here.bat
python -c "import sys; sys.stdout.write(open(sys.argv[1]).read())" %1

and use that tool like

并使用该工具

any2here UNIXFILE.EXT > MYPLATFILE.EXT

回答by uosjead

Building on TampaHaze's and MD XF's helpful answers.

以 TampaHaze 和 MD XF 的有用答案为基础。

This will change all .txt files in placein the current directory from from LF to CRLF in Command Prompt

这将改变所有.txt文件来代替当前目录从LF到CRLF在命令提示符

for /f %f in ('dir /b "*.txt"') do ( type %f | more /p > %f.1 & move %f.1 %f )

If you don't want to verify every single change

如果您不想验证每一个更改

move

移动

To

move /y

移动/y

To include subdirectories change

包含子目录更改

dir /b

目录/b

To

dir /b /s

目录 /b /s

To do all this in a batch file including subdirectories without prompting use below

要在包含子目录的批处理文件中执行所有这些操作而不提示使用以下内容

@echo off
setlocal enabledelayedexpansion

for /f %%f in ('dir /s /b "*.txt"') do (
    type %%f | more /p > %%f.1 
    move /y %%f.1 %%f > nul
    @echo Changing LF-^>CRLF in File %%f
)
echo.
pause

回答by Endoro

try this:

尝试这个:

(for /f "delims=" %i in (file.unix) do @echo %i)>file.dos

Session protocol:

会话协议:

C:\TEST>xxd -g1 file.unix
0000000: 36 31 36 38 39 36 32 39 33 30 38 31 30 38 36 35  6168962930810865
0000010: 0a 34 38 36 38 39 37 34 36 33 32 36 31 38 31 39  .486897463261819
0000020: 37 0a 37 32 30 30 31 33 37 33 39 31 39 32 38 35  7.72001373919285
0000030: 34 37 0a 35 30 32 32 38 31 35 37 33 32 30 32 30  47.5022815732020
0000040: 35 32 34 0a                                      524.

C:\TEST>(for /f "delims=" %i in (file.unix) do @echo %i)>file.dos

C:\TEST>xxd -g1 file.dos
0000000: 36 31 36 38 39 36 32 39 33 30 38 31 30 38 36 35  6168962930810865
0000010: 0d 0a 34 38 36 38 39 37 34 36 33 32 36 31 38 31  ..48689746326181
0000020: 39 37 0d 0a 37 32 30 30 31 33 37 33 39 31 39 32  97..720013739192
0000030: 38 35 34 37 0d 0a 35 30 32 32 38 31 35 37 33 32  8547..5022815732
0000040: 30 32 30 35 32 34 0d 0a                          020524..

回答by Ricardo

My contribution for this, converting several files in a folder: for %%z in (*.txt) do (for /f "delims=" %%i in (%%z) do @echo %%i)>%%z.tmp

我对此的贡献,转换了一个文件夹中的几个文件: for %%z in (*.txt) do (for /f "delims=" %%i in (%%z) do @echo %%i)>%%z.tmp

回答by MD XF

You could create a simple batch script to do this for you:

您可以创建一个简单的批处理脚本来为您执行此操作:

TYPE %1 | MORE /P >%1.1
MOVE %1.1 %1

Then run <batch script name> <FILE>and <FILE>will be instantly converted to DOS line endings.

然后运行<batch script name> <FILE><FILE>会立即转换为 DOS 行尾。

回答by Ir Relevant

Based on Endoro's answer but to keep the blanks, try this:

根据 Endoro 的回答但要保留空白,请尝试以下操作:

@echo off
setlocal enabledelayedexpansion
(for /f "tokens=* delims=:" %%i in ('findstr /n "^" file.unix') do (
        set line=%%i
        set line=!line:*:=!
        echo(!line!
        ))>file.dos