我需要在输出后用jq过滤器提取:
id:1022 mname:P1 target:host.domain.com pname:P1A xnode:1
所有在#
部分中以xstart
开头的pname的对象都应该从processig中忽略。
输入json是:
{
"id": 1022,
"mname": "P1",
"attrib": [
{
"id": 1,
"name": "log",
"value": "1"
},
{
"id": 622,
"name": "target",
"value": "host.domain.com"
},
{
"id": 623,
"name": "iddle",
"value": "30"
}
],
"xstart": [
{
"id": 1021,
"pname": "P1A",
"attrib": [
{
"id": 530,
"name": "xnode",
"value": "1"
},
{
"id": 498,
"name": "remote",
"value": "1"
}
]
},
{
"id": 1022,
"pname": "#P1C",
"attrib": [
{
"id": 530,
"name": "xnode",
"value": "2"
},
{
"id": 498,
"name": "remote",
"value": "1"
}
]
}
]
}
答案 0 :(得分:1)
jq
解决方案:
jq -r '"id:" + (.id|tostring) + " mname:" + .mname
+ " target:" + (.attrib[] | select(.name == "target").value)
+ " pname:" + (.xstart[] | select(.pname | startswith("#") | not) as $x | $x.pname
+ " xnode:" + ($x.attrib[] | select(.name=="xnode").value))' input.json
输出:
id:1022 mname:P1 target:host.domain.com pname:P1A xnode:1
答案 1 :(得分:1)
由于需求主要是隐式指定的,因此很难推断它们,但以下内容可能非常接近,并且无论如何都说明了一些有用的技术,例如{{1}的以下定义中体现的功能的抽象。 }:
kv
使用给定的输入和def kv: [to_entries[] | "\(.key):\(.value)" ] | join(" ");
.attrib |= map(select(.name == "target"))
| .xstart |= map( select(has("pname") and (.pname | startswith("#") | not)))
| {id, mname, target: "host.domain.com"}
+ (.xstart[]
| .pname as $pname
| .attrib[]
| {pname: $pname} + {(.name): .value})
| kv
命令行选项的使用,输出将为:
-r