什么像DOS2Unix for Windows?

Elv*_*vin 15 unix linux windows newline dos2unix

我在Windows上创建了一些shell脚本,我想在它们上运行Dos2Unix.

但正如我已经读到Dos2UnixLinux环境中工作所以,在Windows中工作时,我可以将文件转换为UNIX格式吗?

我已经安装了CYGWIN但是我遇到了一些问题

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
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 11

您可以使用Notepad ++.

递归转换目录的说明如下:

  1. 菜单:搜索 - >在文件中查找...
  2. Directory =递归地要转换为Unix格式的目录.例如,C:\ MyDir
  3. 找到什么=\r \n
  4. 替换为= \n
  5. 搜索模式=扩展
  6. 按"替换文件"

  • @phuclv - 我没有看到原始需求中使用的工具(CLI 或 GUI)有任何限制。此外,Notepad++“编辑 > EOL 转换”是一次仅适用于一个文件的解决方案,而“查找和替换”可以是递归的:在复杂的项目中,它很有用! (4认同)

小智 8

通过 Notepad++ 解决了它。

转到:编辑 -> EOL 转换 -> Unix。


shx*_*hx2 6

如果已perl安装,则只需运行:

perl -i -p -e "s/\r//" <filename> [<filename2> ...]
Run Code Online (Sandbox Code Playgroud)


Mik*_*e T 6

至少有两种资源:

  • SourceForge上的dos2unix似乎已得到积极维护(截至2015年),并且已针对32位和64位Windows进行了预编译。还包括unix2dos,mac2unix和unix2mac。
  • 来自GnuWin32的CygUtils,是Cygwin派生的其他实用程序,其中包括dos2unix以及其他几个相关实用程序。该软件包未得到积极维护(最新更新于2008年)。


phu*_*clv 6

鉴于 .NET 平台上有很多工具,PowerShell 中有很多解决方案

通过文件路径,$file = 'path\to\file'我们可以使用

[IO.File]::WriteAllText($file, $([IO.File]::ReadAllText($file) -replace "`r`n", "`n"))
Run Code Online (Sandbox Code Playgroud)

或者

(Get-Content $file -Raw).Replace("`r`n","`n") | Set-Content $file -Force
Run Code Online (Sandbox Code Playgroud)

-replace "`r", ""也可以用

要对所有文件执行此操作,只需将文件列表通过管道传递给上述命令:

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

对于较大的文件,您可能需要使用使用 powershell 替换 CRLF中的缓冲解决方案