Anything like dos2unix for Windows?

前端 未结 8 1913
栀梦
栀梦 2020-12-01 16:25

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

相关标签:
8条回答
  • 2020-12-01 16:34

    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"
    0 讨论(0)
  • 2020-12-01 16:34

    If you have perl installed, you can simply run:

    perl -i -p -e "s/\r//" <filename> [<filename2> ...]
    
    0 讨论(0)
  • 2020-12-01 16:37

    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".
    0 讨论(0)
  • 2020-12-01 16:44

    Solved it trough Notepad++.

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

    0 讨论(0)
  • 2020-12-01 16:46

    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
    

    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

    • how to convert a file from DOS to Unix
    • How to convert DOS line endings to UNIX on a Windows machine
    • Powershell v2: Replace CRLF with LF

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

    0 讨论(0)
  • 2020-12-01 16:51

    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,

    0 讨论(0)
提交回复
热议问题