我在 stackoverflow 和 jq 手册中阅读了所有答案,但无法找到并理解解决下一个任务需要使用哪些选项:
jq '.result[] | "\(.name) \(.content)"' :
"test.mydomain.com 123.123.123.123"
"static.mydomain.com 124.124.124.124"
"static.mydomain.com 124.124.124.128"
"mydomain.com 125.125.125.125"
"mydomain.com 125.125.125.126"
"mydomain.com 125.125.125.127"
需要从此列表中获取每个第一个值的重复项:
"mydomain.com 125.125.125.125"
"mydomain.com 125.125.125.126"
"mydomain.com 125.125.125.127"
"static.mydomain.com 124.124.124.124"
"static.mydomain.com 124.124.124.128"
抱歉耽误你的时间
最好的问候
最佳答案
让我们使用一个通用实用函数(可能适合您的标准 jq 库)来简化此操作:
# In this formulation, f must either always evaluate to a string or always to an integer
# it being understood that negative integers might be problematic
def aggregate_by(f; g):
reduce .[] as $x (null; .[$x|f] += [$x|g]);
使用 -s(“slurp”)选项,任务现在可以分解为以下步骤:
map(split(" "))
| aggregate_by(.[0]; .[1])
| with_entries(select(.value|length > 1))
| to_entries[]
| .key as $k | .value[] | [$k, .]
| join(" ")
输出(基于原始示例)
"mydomain.com 125.125.125.125"
"mydomain.com 125.125.125.126"
输出(基于修改后的例子)
"static.mydomain.com 124.124.124.124"
"static.mydomain.com 124.124.124.128"
"mydomain.com 125.125.125.125"
"mydomain.com 125.125.125.126"
"mydomain.com 125.125.125.127"
关于json - 如何通过 jq 只获取 json 中的重复键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53553637/