Anything like dos2unix for Windows?

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

    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 /?
    0 讨论(0)
  • 2020-12-01 16:56

    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).
    0 讨论(0)
提交回复
热议问题