Anything like dos2unix for Windows?

Viewed 74563

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

But as I have read that dos2unix works in Linux environment so, is there a way that I can convert my files to UNIX format while working in Windows?

I have already installed CYGWIN but I am facing some issues as

Administrator@SGH735082N ~
$ pwd
/home/Administrator

Administrator@SGH735082N ~
$ cd C:\CVS Code

Administrator@SGH735082N /cygdrive/c/CVS
$ dos2Unix BLPDB000
BLPDB000:
dos2Unix processing BLPDB000: No such file or directory

Administrator@SGH735082N /cygdrive/c/CVS
$ dos2Unix -h
dos2Unix: bad argument -h: unknown option

Administrator@SGH735082N /cygdrive/c/CVS
$ dos2Unix --help
dos2Unix version 0.1.3
  converts the line endings of text files from
  DOS style (0x0d 0x0a) to UNIX style (0x0a)

Usage: dos2Unix [OPTION...] [input file list...]

Main options (not all may apply)
  -A, --auto     Output format will be the opposite of the autodetected source
                 format
  -D, --u2d      Output will be in DOS format
  --unix2dos     Output will be in DOS format
  -U, --d2u      Output will be in UNIX format
  --dos2unix     Output will be in UNIX format
  --force        Ignore binary file detection
  --safe         Do not modify binary files

Help options
  -?, --help     Show this help message
  --usage        Display brief usage message
  --version      Display version information
  --license      Display licensing information

Other arguments
  [input file list...]       for each file listed, convert in place.
                             If none specified, then use stdin/stdout

Administrator@SGH735082N /cygdrive/c/CVS
$

Administrator@SGH735082N /cygdrive/c/CVS
$ dos2Unix  -oBLPDB000
dos2Unix: bad argument -oBLPDB000: unknown option

Administrator@SGH735082N /cygdrive/c/CVS
$ dos2Unix -k BLPDB000
dos2Unix: bad argument -k: unknown option

Administrator@SGH735082N /cygdrive/c/CVS
$ dos2Unix BLPDB000.txt
BLPDB000.txt:
dos2Unix processing BLPDB000.txt: No such file or directory

Administrator@SGH735082N /cygdrive/c/CVS
$ pwd
/cygdrive/c/CVS
11 Answers

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"

Solved it trough Notepad++.

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

If you have perl installed, you can simply run:

perl -i -p -e "s/\r//" <filename> [<filename2> ...]

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

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".
  • Open the 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

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.

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,

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 /?
Related