用于转换 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
Windows command to convert Unix line endings?
提问by Deepti Jain
Is there a Windows command to convert line endings of a file?
是否有 Windows 命令来转换文件的行尾?
We have a test.bat
which 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 more
command which is included in Windows NT and later. To convert input_filename
which contains UNIX EOL (End Of Line) \n
to output_filename
which 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 more
command has additional formatting options that you may not be aware of. Run more/?
to learn what else more
can do.
该more
命令具有您可能不知道的其他格式选项。运行more/?
以了解还能more
做什么。
回答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.vbs
and 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-Content
implicitly splits input files at any kind of linebreak (CR, LF, and CR-LF), and Set-Content
joins 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 CRLF
issues so I decided to build really simple tool for conversion (in NodeJS):
我正在处理CRLF
问题,所以我决定构建非常简单的转换工具(在 NodeJS 中):
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