Linux 像 Windows 的 dos2unix 之类的东西吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20368781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Anything like dos2unix for Windows?
提问by Elvin
I have some shell scripts created on windows I want to run dos2unix
on them.
我在 Windows 上创建了一些 shell 脚本,我想dos2unix
在它们上运行。
But as I have read that dos2unix
works in Linuxenvironment so, is there a way that I can convert my files to UNIX format while working in Windows?
但是,正如我dos2unix
在Linux环境中所读到的那样,有没有一种方法可以在 Windows 中工作时将我的文件转换为 UNIX 格式?
I have already installed CYGWIN but I am facing some issues as
我已经安装了 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
回答by shx2
If you have perl
installed, you can simply run:
如果你已经perl
安装,你可以简单地运行:
perl -i -p -e "s/\r//" <filename> [<filename2> ...]
回答by Erwin Waterlander
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.
您在 Cygwin 上使用的是非常旧的 dos2unix 版本。大约两年前,Cygwin 1.7 更改为新版本的 dos2unix,与大多数 Linux 发行版附带的版本相同。所以用 Cygwin 的安装程序更新你的 dos2unix。检查您是否获得了 6.0.3 版。
There are also native Windows ports of dos2unix available (win32 and win64). See http://waterlan.home.xs4all.nl/dos2unix.html
还有可用的 dos2unix 的本地 Windows 端口(win32 和 win64)。见http://waterlan.home.xs4all.nl/dos2unix.html
regards,
问候,
回答by Mike T
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).
- SourceForge 上的 dos2unix,它似乎得到了积极维护(截至 2015 年),并且已经预编译了适用于 32 位和 64 位 Windows 的版本。还包括 unix2dos、mac2unix 和 unix2mac。
- 来自 GnuWin32 的 CygUtils,它们是从 Cygwin 派生出来的杂项实用程序,其中包括 dos2unix 以及其他几个相关实用程序。这个包没有得到积极维护(上次更新是在 2008 年)。
回答by Ahmad Boorghany
You can use Notepad++.
您可以使用Notepad++。
The instructions to convert a directory recursively are as follows:
递归转换目录的说明如下:
- Menu: Search -> Find in Files...
- Directory = the directory you want to be converted to Unix format, recursively. E.g., C:\MyDir
- Find what = \r\n
- Replace with = \n
- Search Mode = Extended
- Press "Replace in Files"
- 菜单:搜索 -> 在文件中查找...
- Directory = 要递归转换为 Unix 格式的目录。例如,C:\MyDir
- 找出什么 = \r\n
- 替换为 = \n
- 搜索模式 = 扩展
- 按“在文件中替换”
回答by Antonio
I used grepWin:
我使用了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".
- 在 grepWin 中打开包含文件的文件夹
- 在“搜索”部分
- 选择“正则表达式搜索”
- 搜索 ->
\r\n
- 替换为 ->
\n
- 点击“搜索”以确认将触摸哪些文件,然后点击“替换”。
回答by Mofi
Any good text editor on Windows supports saving text files with just line-feed as line termination.
Windows 上任何优秀的文本编辑器都支持仅将换行符保存为行终止符的文本文件。
For an automated conversion of text files from DOS/Windows to UNIX line endings the batch file JREPL.BATcan be used which is written by Dave Benhamand is a batch file / JScript hybrid to run a regular expression replace on a file using JScript working even on Windows XP.
对于文本文件从 DOS/Windows 到 UNIX 行尾的自动转换,可以使用由Dave Benham编写的批处理文件JREPL.BAT,它是一个批处理文件/JScript 混合体,用于使用 JScript 工作在文件上运行正则表达式替换即使在 Windows XP 上。
A single file can be converted from DOS/Windows to UNIX using for example:
例如,可以使用以下命令将单个文件从 DOS/Windows 转换为 UNIX:
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.
在这种情况下,所有回车都从文件中删除以进行修改。当然也可以"\r\n"
用作搜索字符串和"\n"
替换字符串来仅删除换行符的回车符,如果文件在其他地方也包含回车符,而在换行符转换时不应删除这些回车符。
Multiple files of a directory or an entire directory tree can be converted from DOS/Windows to UNIX text files by using command FORto CALLbatch file JREPL.BATon each file matching a wildcard pattern.
目录或整个目录树的多个文件都可以从DOS / Windows的使用命令转换为UNIX文本文件FOR到CALL批处理文件JREPL.BAT上的每个文件匹配的通配符模式。
Batch file example to convert all *.sh files in current directory from DOS/Windows to UNIX.
将当前目录中的所有 *.sh 文件从 DOS/Windows 转换为 UNIX 的批处理文件示例。
@for %%I in (*.sh) do @call "%~dp0jrepl.bat" "\r" "" /M /F "%%I" /O -
The batch file JREPL.BATmust be stored in same directory as the batch file containing this command line.
批处理文件JREPL.BAT必须与包含此命令行的批处理文件存储在同一目录中。
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 /?
jrepl.bat /?
call /?
for /?
回答by kle10
Solved it trough Notepad++.
通过 Notepad++ 解决了它。
Go to: Edit -> EOL Conversion -> Unix.
转到:编辑 -> EOL 转换 -> Unix。
回答by phuclv
In PowerShell there are so many solutions, given a lot of tools in the .NET platform
鉴于 .NET 平台中的大量工具,PowerShell 中有很多解决方案
With a path to file in $file = 'path\to\file'
we can use
有了文件的路径,$file = 'path\to\file'
我们可以使用
[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
对于更大的文件,您可能需要使用Replace CRLF using powershell 中的缓冲解决方案