行端不可知差异?
我正在 Mac 上工作,有一些相当旧的文件。 不同的文件由不同的程序创建,因此其中一些以 \r
(Mac) 结尾,有些以 \n
(Unix) 结尾。 我希望能够在这些文件上运行 diff、grep 等命令,但具有 \r
的文件被视为一大行。 是否有 diff 的版本,grep 等可以与所有换行符一起正常工作吗?
ETA:我还希望它们成为 Unix 实用程序,这样我就可以在脚本、Emacs 等中使用它们......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用
diff -w
它将忽略文件中的空格,这可能足以满足您的需求。编辑:刚刚意识到我第一次读错了这篇文章,而您实际上正在寻找适用于
\r
行结尾的差异。 我的建议是使用 flip 之类的内容转换文件 可以将文件转换为\n
标准格式。编辑2:刚刚找到了一些看起来像你想要的东西 - <强>差异补丁:
If you use
diff -w
it will ignore whitespace in the files, which is probably sufficient for your needs.EDIT: just realized I misread the post the first time and you're actually looking for a diff that will work with
\r
line endings. My suggestion would be to convert the files with something like flip that can convert the files to a\n
standard format.EDIT 2: Just found something that looks like what you want - Diff'nPatch:
正如 Jay 所说,Diff'nPatch 似乎正是您所寻找的。 或者,您可以在单个命令中将所有“\r”行结尾转换为“\n”,如下所示:
或者
(在后一种情况下,您可能希望以某种方式过滤文件列表,否则它将应用于所有所有子目录中的文件。)
As Jay said, Diff'nPatch seems what you are looking for. Alternatively you can convert all your '\r' line endings in '\n' in a single command like this:
or
(You may want to filter the list of files in some way in the latter case or it will be applied to all the files in all subdirectories.)
与 OS X v10.7 (Lion) 捆绑的 diff 实用程序有一个选项 'strip -trailing-cr' 可以满足您的要求。 你像这样使用它:
The diff utility bundled with OS X v10.7 (Lion) has an option 'strip-trailing-cr' that does that you want. You use it like so:
PhpStorm 的 diff 视图的“忽略空白”正常工作。 它会自动忽略回车符/EOL/换行符/what-have-you 中的差异。 你可以浪费时间摆弄晦涩的 Unix 命令或其他什么,或者你可以得到一些真正有效的东西并继续生活。
是的,我的换行符是 \r,但是那又怎样?太愚蠢了,无法意识到 \r == \n 那么我将使用不同的软件, 足够聪明
PhpStorm 是唯一具有“刚刚工作”的 diff 工具的软件 - - 这就是我希望 Mac 软件能够做到的事情。我希望 Mac 软件能够正常工作。 我使用Mac,所以我可以完成我的工作,而不是每次都学习晦涩难懂的终端命令,这些命令几乎都没有很好的文档记录,期望你在没有任何明确示例的情况下理解命令应该如何格式化,所以你永远不知道如果你做错了或者命令就像所有其他糟糕的软件一样根本不起作用。
以“man diff”为例:
好吧,读完这篇文章后,我不知道这意味着什么。 没有其用法的示例。 什么是“RE”? 它没有说任何地方。
然后就是这颗宝石:
我无法理解这段话的任何意义。 什么是“输入”? 是两个文件还是只是“目标”文件或只是“来自”文件? “相似”指的是什么? “GFMT '是' LTYPE 或‘已更改’”这句话中的“是”是什么意思? 这是否意味着“可能被替换”? 如果是这样,那么为什么“GFMT”不在引号、括号等中? 由于没有举出例子,所以无从得知; 该文档的措辞完全不明确。 “GFMT 可能包含”...是什么意思? “包含”是否意味着替换首字母缩略词 GFMT 的文本可能包含该内容? 没有明确的例子,它是完全没有用的。
如果你想让它变得如此神秘和模棱两可,基本上对那些还不知道如何使用该软件的人来说毫无用处,为什么还要费心去写一个手册页呢? 到那时,它就不再是一本手册了;而是一本手册。 它只是为编写该软件的人提供的快速参考页面,以便他们记住如何使用它。 我猜他们认为如果您想知道它的实际用途,您只需阅读源代码本身即可。
我的时间很宝贵。 我宁愿花钱购买一个实际上可以正常工作并且有适当文档的软件。
因为这些都失败了:
...无法忽略 \r 字符。
...无法忽略 \r 字符。
...无法忽略 \r 字符。
...无法忽略 \r 字符。
...无法忽略 \r 字符。
就此而言,如果它们是 \n 字符,则在添加 \n 字符时也会失败。
其中 test.phtml ==
和rest.html ==
“diff”命令总是给你类似的东西:
...失败!
PhpStorm's diff view's "ignore whitespace" just works. It automatically ignores differences in the carriage return / EOL / newline / what-have-you. You can waste your time fiddling with arcane Unix commands or whatever, or you could just get something that actually works and move forward with life.
Yes, my newlines are \r, but so what? Arrr! If the software is too stupid to realize that \r == \n then I'm just going to use different software that is smart enough.
PhpStorm was the only software that had a diff tool that "just worked" -- which is what I expect Mac software to do. I expect Mac software to just work. I use a Mac, so I can do my job instead of learning arcane terminal commands at every turn, which are almost all poorly documented, expecting you to just understand how the commands are supposed to be formatted without any clear examples, so you never know if you're doing it wrong or if the command simply doesn't work just like all other bad software.
Take this example from "man diff":
OK, so having read this, I have no idea what it means. There is no example of its usage. What is "RE"? It doesn't say anywhere.
Then there's this jewel:
I could make no sense whatsoever of this passage. What is the "input"? Is it both files or just the "to" file or just the "from" file? What is "similar" referring to? What does "is" mean in the sentence, "GFMT 'is' LTYPE or `changed'"? Does it mean "may be replaced by"? If so then why isn't "GFMT" in quotations, brackets, etc.? Since no example is given, there is no way to know; the documentation's wording is totally ambiguous. What does "GFMT may contain"... mean? Does "contain" mean that the text replacing the acronym GFMT may contain that? Without a clear example it's completely useless.
Why even bother to write a man page if you're going to make it so cryptic and ambiguous it's useless to anyone who doesn't already know how to use the software, basically? At that point, it's not a manual; it's just a quick-reference page for the guys who wrote the software so they can remember how to use it. I guess they assume you'll just read the source-code itself if you want to know what it actually does.
My time is valuable. I'd rather just pay the money to have a piece of software that actually works correctly and has proper documentation.
Because these all failed:
...failed to ignore \r characters.
...failed to ignore \r characters.
...failed to ignore \r characters.
...failed to ignore \r characters.
...failed to ignore \r characters.
For that matter if they were \n characters it also failed when the \n characters are added.
Where test.phtml ==
and rest.html ==
The "diff" command always gives you something like:
... fail!
dos2unix 命令可能有助于首先将文件转换为一致的格式。 我相信它几乎适用于您能想到的所有平台,并且可以同时运行大量文件。 我相信有一个适用于 Mac 的软件包。
The dos2unix command could be helpful in converting your files to a consistent format first. I believe it's available for just about every platform you can think of and can run on lots of files at once. I believe there's a package available for Mac.
我使用了以下快速修复,它有缺点(见下文):
1:进行比较并仅列出文件名
2:使用编辑器打开并保存每个列出的文件使用过,这将改变行结尾。
3:定期进行比较
缺点包括:
I used the following quick fix, which has drawbacks (see below):
1: Do a diff and list only the filenames
2: Open and save every listed file with the editor that was used, this will change the line-endings.
3: Do a regular diff
Drawbacks include:
这对我有用:
我使用的是 OS X,并且混合了来自 OS X 和 Windows 的文件。
信用:http ://www.codealpha.net/514/diff-and-ignoring-spaces-and-end-of-lines-unix-dos-eol/
This worked for me:
I am on OS X and have mixed files from OS X and Windows.
Credit: http://www.codealpha.net/514/diff-and-ignoring-spaces-and-end-of-lines-unix-dos-eol/