如何比较yaml文件中的键?
有两个 Ruby on Rails 国际化 yaml 文件。一份文件完整,另一份文件缺少密钥。如何比较两个 yaml 文件并查看第二个文件中缺少的键?有没有工具可以做到这一点?
There are two ruby on rails internationalization yaml files. One file is complete and another one is with missing keys. How can I compare two yaml files and see missing keys in second file? Are there any tools for doing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设
file1
是正确的版本,file2
是缺少键的版本:现在
限制:如果您的 YAML 文件包含数组,则当前的实现应该扩展为支持数组。
Assuming
file1
is the proper version andfile2
is the version with missing keys:Now
Limitation: Current implementation should be extended to support arrays if your YAML file contains arrays.
我找不到一个快速的工具来做到这一点。我决定为此编写我的自己的工具。
您可以使用 cabal 安装它:
然后您可以比较两个文件这样:
您还可以比较文件的两个子集:
它的行为与
diff
完全相同,您可以这样做:I couldn't find a fast tool to do that. I decided to write my own tool for this.
You can install it with cabal:
Then you can diff two files this way:
You can also compare two subsets of the files:
It behaves exactly like
diff
, you can do this:differz 比较两个 YAML 文件并打印第二个文件中缺少的键。
它既作为一个库又作为一个命令行工具。您可以使用
gem install Differencez
安装后者。differz compares two YAML files and prints missing keys in the second file.
It comes both as a library and as a command-line tool. You can install the latter with
gem install differz
.我想提取差异以进行处理,而这里的代码片段只是打印内容。所以我的版本返回带有 diff 的哈希值。它的结构反映了原始哈希值,但值是差异的描述。
然后我几乎将它用作:
I wanted to extract the diff to have something to work with, and the snippet here just prints stuff. So my version returns a hash with diff. It's structure mirrors the original hashes, but the values are descriptions of the differences.
I've then used it pretty much as: