Anything like dos2unix for Windows?
Asked Answered
T

11

43

I have some shell scripts created on Windows. I want to run dos2unix on them.

I have read that dos2unix works on Linux.
Is there a way that I can convert my files to having Unix newlines while working on Windows?

Trafficator answered 4/12, 2013 at 6:43 Comment(8)
Install Cygwin to provide a Unix-like environment on Windows. It includes dos2unixLubra
kb.iu.edu/data/acux.htmlColorcast
@JimGarrison: I have update my question Can you help me what wrong I am doing as I havn't used CYGWIN beforeTrafficator
well I get it resolved by giving absolute filepathTrafficator
This question appears to be off-topic because it is about has been resolved. OP wasn't specifying the correct path to the file which was causing the error.Vc
@devnull: how you are saying that question is off topic My question is "Anything like DOS2Unix for Windows?" that no one answered. Working on CYGWIN to run DOs2Unix is not "SOmething like DOS2Unix for Windows".Trafficator
The transcript just shows that you already have dos2unix in cygwin. As cygwin is for windows, that covers what you asked for. The issue on display is that you're inattentive, as you went into a "CVS" folder instead of "CVS Code" (since you didn't quote them, the word Code ended up as a second argument that cd ignored), wrote invalid dos2unix commands after checking its accepted options, and never showed or tested where the file was. Other ways were also suggested, and it's a rare oneliner in Python (python3 -c "open('output.txt', newline='\n').write(open('input.txt').read())").Talkington
Download dos2uniq for Windows from here: waterlan.home.xs4all.nl/dos2unix.htmlApply
G
51

You can use Notepad++.

The instructions to convert a directory recursively are as follows:

  1. Menu: Search -> Find in Files...
  2. Directory = the directory you want to be converted to Unix format, recursively. E.g., C:\MyDir
  3. Find what = \r\n
  4. Replace with = \n
  5. Search Mode = Extended
  6. Press "Replace in Files"
Gimcrack answered 22/11, 2017 at 6:34 Comment(3)
the OP asked about command line utilities, not a GUI program. How can you call Notepad++ to replace in shell scripts? And even in Notepad++ you just need to use edit > EOL conversionRigby
@Rigby - I don't see any restriction about the tool to use (CLI or GUI) in the original demand. Also, Notepad++ "Edit > EOL conversion" is a solution for only one file at a time whereas the "Find and replace" can be recursive: in a complex project, it is useful!Piecemeal
I know it's an old post but this solution is super slow (I had to convert 33GB of files). I used dos2unix (sourceforge.net/projects/dos2unix) and it was ~100 times faster.Kailey
F
19

Solved it trough Notepad++.

Go to: Edit -> EOL Conversion -> Unix.

Fawnia answered 19/7, 2019 at 14:57 Comment(1)
This is very simple and best answer so far.Bagpipe
L
8

If you have perl installed, you can simply run:

perl -i -p -e "s/\r//" <filename> [<filename2> ...]
Lauderdale answered 4/12, 2013 at 7:3 Comment(0)
J
8

There are at least two resources:

  • dos2unix on SourceForge, which appears to be actively maintained (as of 2015), and has pre-compiled releases for Windows, both 32- and 64-bit. Also includes unix2dos, mac2unix, and unix2mac.
  • CygUtils from GnuWin32, which are miscellaneous utilities forked from Cygwin, which includes dos2unix as well as several other related utilities. This package is not actively maintained (last update was in 2008).
Jacey answered 13/8, 2015 at 22:21 Comment(0)
R
7

In PowerShell there are so many solutions, given a lot of tools in the .NET platform

With a path to file in $file = 'path\to\file' we can use

[IO.File]::WriteAllText($file, $([IO.File]::ReadAllText($file) -replace "`r`n", "`n"))

or

(Get-Content $file -Raw).Replace("`r`n","`n") | Set-Content $file -Force

It's also possible to use -replace "`r", "" instead

To do that for all files just pipe the file list to the above commands:

Get-ChildItem -File -Recurse | % { (Get-Content -Raw `
    -Path $_.Fullname).Replace ("`r`n", "`n") | Set-Content -Path $_.Fullname }

See

For bigger files you may want to use the buffering solutions in Replace CRLF using powershell

Rigby answered 4/9, 2019 at 15:46 Comment(0)
C
2

I used grepWin:

  • Open the folder containing your files in grepWin
  • In the "Search for" section
    • select "Regex search"
    • Search for -> \r\n
    • Replace with -> \n
  • Hit "Search" to confirm which files will be touched, then "Replace".
Cryptology answered 8/5, 2018 at 23:9 Comment(0)
C
2
  • Open the mvnw file using Notepad++
  • Hit Ctrl+F
  • Select search mode as "Regular Expression"
  • Search for -> \r\n
  • Replace with -> \n
  • Hit "Replace all" under the "Replace tab"

if the above doesn't work - mvn clean install

Current answered 19/3, 2021 at 7:21 Comment(0)
C
1

Any good text editor on Windows supports saving text files with just line-feed as line termination.

For an automated conversion of text files from DOS/Windows to UNIX line endings the batch file JREPL.BAT can be used which is written by Dave Benham and is a batch file / JScript hybrid to run a regular expression replace on a file using JScript working even on Windows XP.

A single file can be converted from DOS/Windows to UNIX using for example:

jrepl.bat "\r" "" /M /F "Name of File to Modify" /O -

In this case all carriage returns are removed from the file to modify. It would be of course also possible to use "\r\n" as search string and "\n" as replace string to remove only a carriage return left to a line-feed if the file contains carriage returns also somewhere else which should not be removed on conversion of the line terminators.

Multiple files of a directory or an entire directory tree can be converted from DOS/Windows to UNIX text files by using command FOR to CALL batch file JREPL.BAT on each file matching a wildcard pattern.

Batch file example to convert all *.sh files in current directory from DOS/Windows to UNIX.

@for %%I in (*.sh) do @call "%~dp0jrepl.bat" "\r" "" /M /F "%%I" /O -

The batch file JREPL.BAT must be stored in same directory as the batch file containing this command line.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • jrepl.bat /?
  • call /?
  • for /?
Capitulum answered 8/7, 2019 at 14:14 Comment(0)
M
1

The search and replace Regex didn't work for me for whatever reason, however solved on single file (~/.bashrc) in Notepad++ by setting Encoding --> UTF-8 and resaving. Not as scalable but hopefully saves some headaches for quick conversion.

Matthieu answered 16/4, 2021 at 20:51 Comment(0)
F
1

I realize this may be a bit of a contextual leap, but I'll share my thought anyway since it just helped in my use case...

If the file will live in a git repo, you can enforce the line endings on it via a .gitattributes file. See: how to make git not change line endings for one particular file?

Feudalism answered 29/12, 2021 at 1:6 Comment(0)
A
0

You are using a very old dos2unix version on Cygwin. Cygwin 1.7 changed to a new version of dos2unix, the same as is shipped with most Linux distributions, about two years ago. So update your dos2unix with Cygwin's setup program. Check you get version 6.0.3.

There are also native Windows ports of dos2unix available (win32 and win64). See http://waterlan.home.xs4all.nl/dos2unix.html

regards,

Anthology answered 4/12, 2013 at 8:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.