Windows command to convert Unix line endings?
Solution 1
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:
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.
Solution 2
Use unix2dos utility. You can download binaries here.
Solution 3
I was dealing with CRLF
issues so I decided to build really simple tool for conversion (in NodeJS):
So if you have NodeJS with npm installed you can try it:
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).
So if you can use NodeJS, it's really simple and you can integrate this command to convert whole workspace to desired line endings.
Solution 4
You can do this without additional tools in 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:
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:
(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.
Solution 5
Windows' MORE is not reliable, it destroys TABs inevitably and adds lines.
unix2dos is part also of MinGW/MSYS, Cygutils, GnuWin32 and other unix binary port collections - and may already be installed.
When python is there, this one-liner converts any line endings to current platform - on any platform:
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:
@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
Related videos on Youtube
Deepti Jain
Updated on July 05, 2022Comments
-
Deepti Jain almost 2 years
Is there a Windows command to convert line endings of a file?
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.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?
Thanks in advance.
-
Deepti Jain almost 11 yearsAgain its just an alternative to notepad++. Won't really get this automated. I will have to run this tool. I do change the line endings in notepad++ as of now.
-
Ansgar Wiechers almost 11 yearsActually, it would work if he'd simply use something like
(for /f "tokens=*" %%a in (%~1) do @echo.%%a)>"%~2"
. -
David Jashi almost 11 yearsIt's a command line tool. And it's more suitable for automation, then GUI tools. You may write a periodical
at
job, which filters all files in some folder you put your batch files in. It all depends on what do you mean by automation. -
Endoro almost 11 years@AnsgarWiechers Yes, this
set TargetFile=%2 rem c:\test\out.txt
also needs improvement. And, btw, your"tokens=*"
also. -
Jimmy Smith almost 11 years@Endoro REM is a comment in a batch file, you can take out the %1 rem and then give it the specific file you need. As I provided the link, I didn't feel I needed to further document as you were already working with a batch file, correct? Please test it as it does work and it can be integrated into your existing batch files at will. Thanks!
-
Jimmy Smith almost 11 yearsWorks fine for me in Windows 7. I created a file c:\test\test.txt, with EOL from Unix, in Notepad++. Afterwards, I simply ran the batch file. Again, it's not my code, it is code I slightly modified to not display Notepad? I can show the OP how to make it work as opposed to that last comment.
-
MarioVilas over 10 yearsIt works, but it also seems to remove all blank lines from the file. :(
-
Endoro over 10 years@MarioVilas yes, you are right. You can use flip for this.
-
mgcoder about 9 yearsIr Relevant's solution works fine except it drop all exclamation mark (!).
-
SCBuergel over 8 yearsthis still works great in Windows 9, thanks! Might be worth mentioning that the file output_filename has to be existing before running this command and its content will be overwritten.
-
aolszowka about 8 yearsThis seems to add an additional Newline to files that do not end with a newline, just a heads up if this matters for your particular scenario.
-
Colin over 7 yearsUnfortunately this also botches all the tabs to spaces.
-
Ross Brigoli about 7 yearsI need the opposite of this. any idea?
-
Dimagog about 7 yearsGreat answer! Slightly simpler:
more /P <input_file >output_file
-
MD XF almost 7 years@Sebastian What's Windows 9?
-
Mikhail over 6 yearsLooks like your solution hit reddit.com/r/programmingcirclejerk/comments/6t3kel/…
-
Andrew Steitz over 6 years@adrianTNT, you did
TYPE input_file | MORE /P > input_file
-
Franklin Yu about 6 years@AndrewSteitz He wanted an in-place conversion.
-
Andrew Steitz about 6 years@FranklinYu, you are correct. I was perplexed by your comment at first and then I realized that in my previous comment I forgot the "?" because I was asking (very poorly) if he actually typed that on the command line. Doh! I was asking (well, meant to ask) him because I was sure that it worked and even tried it to be 100% certain. Punctuation matters (and I goofed). I like the example "Can we eat, Grandma?" vs "Can we eat Grandma?" (1st, ask Grandma if we can eat. 2nd, asking someone else if we can eat Grandma, LOL). Thank you for pointing out my mistake so I could clarify!!!
-
Ansgar Wiechers about 6 years@FranklinYu With the PowerShell approach you can write the modified content back to the same file (the parentheses around
Get-Content
make that possible). As for "in-place" editing in general: see here. -
js2010 over 5 yearsFor that first powershell method with -replace, you would have to add the -raw option to get-content to do what you intend. Just don't do it twice on the same file. You don't even need to quote the filenames. Note that using ">" or "out-file" in powershell 5 results in a "unicode" encoded file vs "ansi".
-
Ziumper almost 5 yearsThere is a way to use git to convert CRLF to LF i just used your approach to get LF ending, but i can not get it working. so i decided to change git configs here is a snippet: $ git config --global core.autocrlf input
-
jeb over 4 yearsThis drops all colons
:
from line beginnings. That destroys all batch functions. Btw. Test to convert a file with a line like\..\..\windows\system32\calc.exe
. To solve this look at this explanation -
Jean-François Larvoire about 4 yearsYou're right! I wanted to do a 1-liner, simpler and better than the previously published solutions, but apparently this cannot be done. Yours is the only correct so far.
-
Jean-François Larvoire about 4 yearsBut the challenge was irresistible, and maybe I have found another 1-line solution now, as shown in the edited text above :-)
-
jeb over 3 yearsAnd you failed again :-) Try it with
..\..\..\..\..\windows\system32\calc.exe
. The only safe ECHO form is theecho(
. A one liner could be possible, but I suppose it's tricky -
Jean-François Larvoire over 3 yearsCaramba, schon wieder verpaßt! OK, I give up for now. But I'm sure that one day, I'll make it! :-)
-
John Smith almost 3 yearsJeez, how many "ifs, ands, or buts" does Windows make things require? I can't follow any of this. I never thought the single most time consuming part of developing a whole custom PHP database view would be dealing with the line endings after copying the finished project to our server. Is there no Windows utility than can just fix this extremely common problem without all this headscratching?
-
John Smith almost 3 yearsNope, this fails completely if there is a space in the filepath, such as, say, "C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\".
-
John Smith almost 3 yearsUPDATE: Did it with Notepad++.
-
Jean-François Larvoire almost 3 years@John Smith: There are lots of ports of the unix2dos and dos2unix programs for Windows, which easily resolve the initial problem. See kxr's answer below stackoverflow.com/a/35661818/2215591 for details. Here, purely for the sake of the art, I was trying to do it using a short Windows batch script. This is very hard to do in that poor scripting language, if possible at all. But this very difficulty is why some of us find it a fun challenge. :-)