我在Windows上创建了一些shell脚本,我想在它们上运行 Dos2Unix 。
但正如我已经读到 Dos2Unix 在 Linux 环境中工作所以,在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
由于
答案 0 :(得分:9)
您可以使用Notepad++。
递归转换目录的说明如下:
答案 1 :(得分:6)
如果您安装了perl
,则只需运行:
perl -i -p -e "s/\r//" <filename> [<filename2> ...]
答案 2 :(得分:4)
至少有两种资源:
答案 3 :(得分:1)
我使用grepWin:
\r\n
\n
答案 4 :(得分:0)
您在Cygwin上使用的是非常旧的dos2unix版本。大约两年前,Cygwin 1.7改为dos2unix的新版本,与大多数Linux发行版一样。所以用Cygwin的安装程序更新你的dos2unix。检查您是否获得版本6.0.3。
还有dos2unix的本机Windows端口可用(win32和win64)。 见http://waterlan.home.xs4all.nl/dos2unix.html
的问候,
答案 5 :(得分:0)
Windows上任何优秀的文本编辑器都支持仅将换行符作为行终止符来保存文本文件。
要将文本文件从DOS / Windows自动转换为UNIX行结尾,可以使用批处理文件JREPL.BAT,该批处理文件由 Dave Benham 编写,是批处理文件/ JScript混合格式甚至在Windows XP上也可以使用JScript在文件上运行正则表达式替换。
可以使用以下命令将单个文件从DOS / Windows转换为UNIX:
jrepl.bat "\r" "" /M /F "Name of File to Modify" /O -
在这种情况下,所有回车都从文件中删除以进行修改。当然,也可以使用"\r\n"
作为搜索字符串,并使用"\n"
作为替换字符串,如果文件中还包含回车符,则仅删除留在换行符中的回车符在转换行终止符时将其删除。
可以使用命令 FOR 到 CALL 批处理文件 JREPL,将目录或整个目录树的多个文件从DOS / Windows转换为UNIX文本文件。 .BAT 在与通配符模式匹配的每个文件上。
批处理文件示例,用于将当前目录中的所有* .sh文件从DOS / Windows转换为UNIX。
@for %%I in (*.sh) do @call "%~dp0jrepl.bat" "\r" "" /M /F "%%I" /O -
批处理文件 JREPL.BAT 必须与包含此命令行的批处理文件存储在同一目录中。
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。
jrepl.bat /?
call /?
for /?
答案 6 :(得分:0)
通过记事本++解决。
转到:编辑-> EOL转换-> Unix。
答案 7 :(得分:0)
在PowerShell中,.NET平台中提供了许多工具,因此有很多解决方案
我们可以使用$file = 'path\to\file'
中的文件路径
[IO.File]::WriteAllText($file, $([IO.File]::ReadAllText($file) -replace "`r`n", "`n"))
或
(Get-Content $file -Raw).Replace("`r`n","`n") | Set-Content $file -Force
要对所有文件执行此操作,只需将文件列表传递到上述命令即可:
Get-ChildItem -File -Recurse | % { (Get-Content -Raw -Path $_.Fullname).Replace ("`r`n", "`n") | Set-Content -Path $_.Fullname }
请参见
对于更大的文件,您可能需要使用Replace CRLF using powershell
中的缓冲解决方案答案 8 :(得分:0)
\r\n
\n
答案 9 :(得分:0)
无论出于何种原因,搜索和替换正则表达式对我都不起作用,但是通过设置编码 --> UTF-8 并重新保存在 Notepad++ 中的单个文件 (~/.bashrc) 上解决了。不具备可扩展性,但有望为快速转换省去一些麻烦。