macos 线端不可知差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/543779/
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
Line-end agnostic diff?
提问by Brian Postow
I'm working on a Mac, with some fairly old files. Different files were created by different programs, so some of them end with \r (Mac) and some with \n (Unix). I want to be able to run commands like diff, grep, etc. on these files, but the ones that have \r are treated as one giant line. Is there of a version of diff, grep, etc. that will work correctly with all new-lines?
我在 Mac 上工作,有一些相当旧的文件。不同的文件是由不同的程序创建的,所以有些文件以 \r (Mac) 结尾,有些以 \n (Unix) 结尾。我希望能够在这些文件上运行 diff、grep 等命令,但那些具有 \r 的命令被视为一个巨大的行。是否有diff、grep等版本可以与所有换行符一起正常工作?
ETA: I'd also like them to be Unix utilities, so I can use them in scripts, Emacs, etc...
ETA:我也希望它们是 Unix 实用程序,这样我就可以在脚本、Emacs 等中使用它们......
采纳答案by UncleZeiv
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:
正如 Jay 所说,Diff'nPatch 似乎正是您要找的。或者,您可以在单个命令中将所有 '\r' 行结尾转换为 '\n',如下所示:
sed -ie 's/\r/\n/' filename
or
或者
find . | xargs -n1 sed -ie 's/\r/\n/'
(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.)
(在后一种情况下,您可能希望以某种方式过滤文件列表,否则它将应用于所有子目录中的所有文件。)
回答by Jay
If you use diff -w
it will ignore whitespace in the files, which is probably sufficient for your needs.
如果您使用diff -w
它将忽略文件中的空格,这可能足以满足您的需要。
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 flipthat can convert the files to a \n
standard format.
编辑:刚刚意识到我第一次误读了这篇文章,而您实际上是在寻找一个适用于\r
行尾的差异。我的建议是将文件转换的东西,如翻转,可以将文件转换为\n
标准格式。
EDIT 2: Just found something that looks like what you want - Diff'nPatch:
编辑 2:刚刚找到了一些看起来像你想要的东西 - Diff'nPatch:
Diff'nPatch is a port to the Macintosh of the GNU 'diff', 'patch' and 'cmp' utilities. It lets you compare and find differences between two files or folders, collate two files, generate diffs in various formats (normal, context, unidiff, etc.), apply patches, compare files byte by byte. It can handle any type of line endings (mac, unix or windows)
Diff'nPatch 是 GNU 'diff'、'patch' 和 'cmp' 实用程序的 Macintosh 端口。它使您可以比较和查找两个文件或文件夹之间的差异、整理两个文件、生成各种格式(正常、上下文、unidiff 等)的差异、应用补丁、逐字节比较文件。它可以处理任何类型的行尾(mac、unix 或 windows)
回答by Karra
The diff utility bundled with OSX Lion has an option 'strip-trailing-cr' that does that you want. You use it like so:
与 OSX Lion 捆绑的 diff 实用程序有一个选项“strip-trailing-cr”,可以满足您的需求。你像这样使用它:
diff -cpt a.c b.c --strip-trailing-cr
回答by CommaToast
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.
PHPStorm 的差异视图的“忽略空格”才有效。它会自动忽略回车/EOL/换行符/what-have-you 中的差异。您可以浪费时间摆弄神秘的 unix 命令或其他任何东西,或者您可以获得一些实际有效的东西并继续前进。
- Using any of the above-mentioned solutions failed on Mountain Lion (including the one marked as the correct answer). All the download links for "Diff-npatch" failed. (I did find http://webperso.easyconnect.fr/bdesgraupes/tools.htmlbut I really don't like the idea of having to resort to using a diff tool that cannot be invoked from the command line and thus integrated with whatever IDE or VCS tool I might be using, like BBEdit, SourceTree, or SmartSVN -- all of which, BTW, failed to ignore newlines with their built-in diff tool.
- 在 Mountain Lion 上使用上述任何一种解决方案都失败了(包括标记为正确答案的那个)。“Diff-npatch”的所有下载链接都失败了。(我确实找到了http://webperso.easyconnect.fr/bdesgraupes/tools.html但我真的不喜欢不得不求助于使用无法从命令行调用的差异工具的想法,因此与任何我可能正在使用的 IDE 或 VCS 工具,如 BBEdit、SourceTree 或 SmartSVN——顺便说一句,所有这些都未能通过其内置的差异工具忽略换行符。
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 issmart enough.
是的,我的换行符是 \r,但那又怎样?啊!如果软件是太愚蠢意识到\ r == \ n然后我只是要使用不同的软件,它是足够聪明。
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 workjust like all other bad software. Take this example from "man diff":
PHPStorm 是唯一具有“正常工作”的差异工具的软件——这正是我期望 Mac 软件能够做到的。我希望 Mac 软件能够正常工作。我使用 Mac,所以我可以完成我的工作,而不是每次都学习神秘的终端命令,这些命令几乎都没有很好的文档记录,希望您在没有任何明确示例的情况下只了解命令的格式,所以您永远不知道你做错了,或者命令根本不能像所有其他坏软件一样工作。以“man diff”为例:
-I RE --ignore-matching-lines=RE Ignore changes whose lines all match RE.
-I RE --ignore-matching-lines=RE Ignore changes whose lines all match RE.
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.
好吧,读完这篇,我不知道它是什么意思。没有使用示例。什么是“RE”?它没有说任何地方。
Then there's this jewel:
然后是这颗宝石:
--GTYPE-group-format=GFMT Similar, but format GTYPE input groups with GFMT. --line-format=LFMT Similar, but format all input lines with LFMT. --LTYPE-line-format=LFMT Similar, but format LTYPE input lines with LFMT. LTYPE is `old', `new', or `unchanged'. GTYPE is LTYPE or `changed'. GFMT may contain: %< lines from FILE1 %> lines from FILE2 %= lines common to FILE1 and FILE2 %[-][WIDTH][.[PREC]]{doxX}LETTER printf-style spec for LETTER LETTERs are as follows for new group, lower case for old group: F first line number L last line number N number of lines = L-F+1 E F-1 M L+1 LFMT may contain: %L contents of line %l contents of line, excluding any trailing newline %[-][WIDTH][.[PREC]]{doxX}n printf-style spec for input line number Either GFMT or LFMT may contain: %% % %c'C' the single character C %c'\OOO' the character with octal code OOO
--GTYPE-group-format=GFMT Similar, but format GTYPE input groups with GFMT. --line-format=LFMT Similar, but format all input lines with LFMT. --LTYPE-line-format=LFMT Similar, but format LTYPE input lines with LFMT. LTYPE is `old', `new', or `unchanged'. GTYPE is LTYPE or `changed'. GFMT may contain: %< lines from FILE1 %> lines from FILE2 %= lines common to FILE1 and FILE2 %[-][WIDTH][.[PREC]]{doxX}LETTER printf-style spec for LETTER LETTERs are as follows for new group, lower case for old group: F first line number L last line number N number of lines = L-F+1 E F-1 M L+1 LFMT may contain: %L contents of line %l contents of line, excluding any trailing newline %[-][WIDTH][.[PREC]]{doxX}n printf-style spec for input line number Either GFMT or LFMT may contain: %% % %c'C' the single character C %c'\OOO' the character with octal code OOO
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 or 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.
我对这段话毫无意义。什么是“输入”?它是两个文件还是只是“to”文件或只是“from”文件?“相似”指的是什么?“GFMT 'is' LTYPE 或'changed'”在句子中是什么意思?是“可以被替换”的意思吗?如果是这样,那么为什么引号或括号等中没有“GFMT”?既然没有给出例子,也就无从得知;文档的措辞完全模棱两可。“GFMT 可能包含”是什么意思?“包含”是否意味着替换首字母缩略词 GFMT 的文本可能包含该内容?没有一个明确的例子,它是完全没用的。
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:
因为这些都失败了:
diff -d --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
...failed to ignore \r characters.
...未能忽略 \r 字符。
diff -wd --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
...failed to ignore \r characters.
...未能忽略 \r 字符。
diff -wd --suppress-common-lines --strip-trailing-cr --ignore-all-space --from-file=rest.phtml test.phtml
...failed to ignore \r characters.
...未能忽略 \r 字符。
diff -wd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
...failed to ignore \r characters.
...未能忽略 \r 字符。
diff -awd test.phtml rest.phtml --suppress-common-lines --strip-trailing-cr --ignore-all-space
...failed to ignore \r characters.
...未能忽略 \r 字符。
For that matter if they were \n characters it also failed when the \n characters are added.
就此而言,如果它们是 \n 字符,它也会在添加 \n 字符时失败。
Where test.phtml ==
哪里 test.phtml ==
foo
bar
富
酒吧
and rest.html ==
和rest.html ==
foobar
食物吧
The "diff" command always gives you something like:
“diff”命令总是给你类似的东西:
* 1,2 **! foo ! bar \ No newline at end of file
--- 1 ---- ! foobar \ No newline at end of file
* 1,2 **!福!bar \ 文件末尾没有换行符
--- 1 --- !foobar \ 文件末尾没有换行符
... fail!
... 失败!
回答by Rog
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.
dos2unix 命令可能有助于首先将文件转换为一致的格式。我相信它几乎适用于您能想到的每个平台,并且可以同时在大量文件上运行。我相信有一个适用于 mac 的软件包。
回答by Niels Bom
I used the following quick fix, which has drawbacks (see below):
我使用了以下快速修复,它有缺点(见下文):
1: Do a diff and list only the filenames
1:做一个差异并只列出文件名
diff -r -q dir1/ dir2/
2: Open and save every listed file with the editor that was used, this will change the line-endings.
2:使用所使用的编辑器打开并保存每个列出的文件,这将更改行尾。
3: Do a regular diff
3:做一个常规的差异
Drawbacks include:
缺点包括:
- less robust, error prone
- more work if you have lots of files
- 不太稳健,容易出错
- 如果您有很多文件,则需要更多的工作
回答by Wisco crew
This worked for me:
这对我有用:
diff -r --ignore-all-space dir1/ dir2/
I am on OSX, and have mixed files from OSX and windows. Credit: http://www.codealpha.net/514/diff-and-ignoring-spaces-and-end-of-lines-unix-dos-eol/
我在 OSX 上,并且有来自 OSX 和 Windows 的混合文件。信用:http: //www.codealpha.net/514/diff-and-ignoring-spaces-and-end-of-lines-unix-dos-eol/