Macos 线端不可知差异?
我在Mac电脑上工作,有一些相当旧的文件。不同的程序创建了不同的文件,因此有些文件以\r(Mac)结尾,有些文件以\n(Unix)结尾。我希望能够在这些文件上运行diff、grep等命令,但是那些具有\r的文件被视为一行。是否有一个版本的等,将正确地与所有新的行Macos 线端不可知差异?,macos,unix,diff,utilities,Macos,Unix,Diff,Utilities,我在Mac电脑上工作,有一些相当旧的文件。不同的程序创建了不同的文件,因此有些文件以\r(Mac)结尾,有些文件以\n(Unix)结尾。我希望能够在这些文件上运行diff、grep等命令,但是那些具有\r的文件被视为一行。是否有一个版本的等,将正确地与所有新的行 ETA:我还希望它们是Unix实用程序,因此我可以在脚本、Emacs等中使用它们。如果使用diff-w它将忽略文件中的空白,这可能足以满足您的需要 编辑:刚意识到我第一次误读了这篇文章,而你实际上是在寻找一个可以处理\r行结尾的差异。我
ETA:我还希望它们是Unix实用程序,因此我可以在脚本、Emacs等中使用它们。如果使用
diff-w
它将忽略文件中的空白,这可能足以满足您的需要
编辑:刚意识到我第一次误读了这篇文章,而你实际上是在寻找一个可以处理\r
行结尾的差异。我的建议是使用类似的东西来转换文件,这样可以将文件转换为\n
标准格式
编辑2:刚刚找到与您想要的内容相似的内容-:
Diff'nPatch是Macintosh的一个端口
GNU的“差异”、“补丁”和“cmp”
公用事业。它可以让你进行比较和分析
查找两个文件或文件之间的差异
文件夹,整理两个文件,生成
不同格式的差异(正常,
上下文、unidiff等),适用
修补程序,逐字节比较文件。
它可以处理任何类型的线端点
(mac、unix或windows)
dos2unix命令可能有助于首先将文件转换为一致的格式。我相信它适用于你能想到的几乎所有平台,并且可以同时运行在很多文件上。我相信Mac有一个可用的软件包。正如Jay所说,Diff'nPatch似乎就是你想要的。或者,您可以在一个命令中转换“\n”中的所有“\r”行结尾,如下所示:
sed -ie 's/\r/\n/' filename
或
(在后一种情况下,您可能希望以某种方式过滤文件列表,或者它将应用于所有子目录中的所有文件。)我使用了以下快速修复方法,但有缺点(请参见下文): 1:进行区分,只列出文件名
diff -r -q dir1/ dir2/
2:使用所用编辑器打开并保存每个列出的文件,这将更改行尾
3:定期进行差异
缺点包括:
- 不太健壮,容易出错
- 如果你有很多文件,你需要做更多的工作
diff -cpt a.c b.c --strip-trailing-cr
这对我很有用:
diff -r --ignore-all-space dir1/ dir2/
我在OSX上,有来自OSX和Windows的混合文件
信用证:PhpStorm的diff-view的“忽略空白”只起作用。它会自动忽略回车/下线/换行/您拥有的内容之间的差异。你可以浪费时间去摆弄那些晦涩难懂的Unix命令或其他什么,或者你可以得到一些真正有用的东西,并继续生活下去
- 在OS X v10.8(Mountain Lion)上使用上述任何解决方案都失败(包括标记为正确答案的解决方案)。“Diff npatch”的所有下载链接均失败。(我确实发现了,但我真的不喜欢使用不能从命令行调用的diff工具,从而与我可能使用的任何IDE或版本控制系统工具(比如SmartSVN)集成的想法——顺便说一句,所有这些工具都无法用内置的diff工具忽略换行符
diff -wd --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd --suppress-common-lines --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
diff -awd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
…未能忽略\r\n个字符
diff -wd --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd --suppress-common-lines --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
diff -awd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
…未能忽略\r\n个字符
diff -wd --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd --suppress-common-lines --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
diff -awd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
…未能忽略\r\n个字符
diff -wd --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd --suppress-common-lines --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
diff -awd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
…未能忽略\r\n个字符
diff -wd --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd --suppress-common-lines --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
diff -wd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
diff -awd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
因此,如果它们是\n个字符,则在添加\n个字符时也会失败
Where test.phtml==
福
酒吧
和rest.html==
福巴
“diff”命令始终提供如下信息:
***1,2****!foo!bar\n文件末尾没有换行符 ---1-->foobar\n文件末尾没有换行符
…失败!nope.-w只是忽略了行中的空白。然后它将\r文件视为一个巨大的行,没有\r。文件仍然是一堆不同的行。似乎Diff'nPatch的链接现在是我同意Chris的看法:我刚找到Jay的答复。我还对不同的行结尾有问题,结果是
Diff
将文件的每一行标记为差异。我比较了DOS/*X文件。diff-uw
是解决方法。对于许多其他程序,换行符也是一个空白字符,但diff是一个面向行的程序,换行符总是以行结尾。因此-w或--ignore all space选项不会忽略与换行符相关的更改;它只忽略其他空白更改。dos2unix中通过convmode选项支持mac格式的文件。考虑到这一点,可以创建一致(和单独)的转换为diffing/grepping。前一段时间我写了一个mac2unix,这是最好的解决方案……还有一个命令