【发布时间】:2017-11-26 00:41:08
【问题描述】:
我正在与 JQ 合作,到目前为止我非常喜欢它。不过,我遇到了一个问题,我还没有找到其他任何地方的解决方案,我想看看社区是否有办法做到这一点。
假设我们有一个如下所示的 JSON 文件:
{"author": "Gary", "text": "Blah"}
{"author": "Larry", "text": "More Blah"}
{"author": "Jerry", "text": "Yet more Blah"}
{"author": "Barry", "text": "Even more Blah"}
{"author": "Teri", "text": "Text on text on text"}
{"author": "Bob", "text": "Another thing to say"}
现在,我们要选择author 的值等于“Gary”或“Larry”的行,但没有其他情况。实际上,我要检查数千个名称,因此仅说明直接或条件(例如cat blah.json | jq -r 'select(.author == "Gary" or .author == "Larry")')是不够的。我正在尝试通过 inside 函数执行此操作,但得到一个错误对话框:
cat blah.json | jq -r 'select(.author | inside(["Gary", "Larry"]))'
jq: error (at <stdin>:1): array (["Gary","La...) and string ("Gary") cannot have their containment checked
做这样的事情最好的方法是什么?
【问题讨论】:
-
你不需要
inside函数,你可以得到select(.author == ("Gary", "Larry"))这样的条目