Anything like dos2unix for Windows?

前端 未结 8 1934
栀梦
栀梦 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: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

提交回复
热议问题