Windows command to convert Unix line endings?

142,594

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):

It's NodeJS EOL converter CLI

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
Share:
142,594

Related videos on Youtube

Deepti Jain
Author by

Deepti Jain

Updated on July 05, 2022

Comments

  • Deepti Jain
    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
    Deepti Jain almost 11 years
    Again 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
    Ansgar Wiechers almost 11 years
    Actually, it would work if he'd simply use something like (for /f "tokens=*" %%a in (%~1) do @echo.%%a)>"%~2".
  • David Jashi
    David Jashi almost 11 years
    It'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
    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
    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
    Jimmy Smith almost 11 years
    Works 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
    MarioVilas over 10 years
    It works, but it also seems to remove all blank lines from the file. :(
  • Endoro
    Endoro over 10 years
    @MarioVilas yes, you are right. You can use flip for this.
  • mgcoder
    mgcoder about 9 years
    Ir Relevant's solution works fine except it drop all exclamation mark (!).
  • SCBuergel
    SCBuergel over 8 years
    this 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
    aolszowka about 8 years
    This 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
    Colin over 7 years
    Unfortunately this also botches all the tabs to spaces.
  • Ross Brigoli
    Ross Brigoli about 7 years
    I need the opposite of this. any idea?
  • Dimagog
    Dimagog about 7 years
    Great answer! Slightly simpler: more /P <input_file >output_file
  • MD XF
    MD XF almost 7 years
    @Sebastian What's Windows 9?
  • Mikhail
    Mikhail over 6 years
  • Andrew Steitz
    Andrew Steitz over 6 years
    @adrianTNT, you did TYPE input_file | MORE /P > input_file
  • Franklin Yu
    Franklin Yu about 6 years
    @AndrewSteitz He wanted an in-place conversion.
  • Andrew Steitz
    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
    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
    js2010 over 5 years
    For 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
    Ziumper almost 5 years
    There 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
    jeb over 4 years
    This 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
    Jean-François Larvoire about 4 years
    You'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
    Jean-François Larvoire about 4 years
    But the challenge was irresistible, and maybe I have found another 1-line solution now, as shown in the edited text above :-)
  • jeb
    jeb over 3 years
    And you failed again :-) Try it with ..\..\..\..\..\windows\system32\calc.exe. The only safe ECHO form is the echo(. A one liner could be possible, but I suppose it's tricky
  • Jean-François Larvoire
    Jean-François Larvoire over 3 years
    Caramba, schon wieder verpaßt! OK, I give up for now. But I'm sure that one day, I'll make it! :-)
  • John Smith
    John Smith almost 3 years
    Jeez, 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
    John Smith almost 3 years
    Nope, this fails completely if there is a space in the filepath, such as, say, "C:\Program Files\FileMaker\FileMaker Server\HTTPServer\conf\".
  • John Smith
    John Smith almost 3 years
    UPDATE: Did it with Notepad++.
  • Jean-François Larvoire
    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. :-)