Select 不同级别的多个字段
Select multiple fields at different levels
我有一个 .json 文件,看起来像 :
[
{
"network": "X.X.X.1",
"defaultGateway": "X.X.X.X",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.2",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.3",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.4",
"foo3": "available"
},
{
"foo1": "10.0.0.5",
"foo3": "available"
},
{
"foo1": "10.0.0.6",
"foo3": "available"
},
{
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
"id": "xxx"
},
{
"network": "X.X.X.2",
"defaultGateway": "X.X.X.X",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.2",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.3",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.4",
"foo3": "available"
},
{
"foo1": "10.0.0.5",
"foo3": "available"
},
{
"foo1": "10.0.0.6",
"foo3": "available"
},
{
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
"id": "xxx"
}
]
我尝试的第一件事就是像这样显示每个元素:
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab}'
显示文件的每一行。
现在,我想显示一些行:
- 网络
- ipAddressTab
- foo1
- foo3
{
"network": "X.X.X.1",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo3": "reserved",
"foo1": "10.0.0.2",
"foo3": "reserved",
"foo1": "10.0.0.3",
"foo3": "reserved",
"foo1": "10.0.0.4",
"foo3": "available"
"foo1": "10.0.0.5",
"foo3": "available"
"foo1": "10.0.0.6",
"foo3": "available"
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
},
{
"network": "X.X.X.2",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo3": "reserved",
"foo1": "10.0.0.2",
"foo3": "reserved",
"foo1": "10.0.0.3",
"foo3": "reserved",
"foo1": "10.0.0.4",
"foo3": "available"
"foo1": "10.0.0.5",
"foo3": "available"
"foo1": "10.0.0.6",
"foo3": "available"
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
}
]
我试过类似的东西:
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab(.foo1, .foo3)}'
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab[.foo1, .foo3]}'
但是没用!
谁能告诉我如何显示我需要的每个元素?
非常感谢!
首先select字段,然后修改ipAddressTab
。
map({network, ipAddressTab, full}
| .ipAddressTab[] |= {foo1, foo3})
我有一个 .json 文件,看起来像 :
[
{
"network": "X.X.X.1",
"defaultGateway": "X.X.X.X",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.2",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.3",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.4",
"foo3": "available"
},
{
"foo1": "10.0.0.5",
"foo3": "available"
},
{
"foo1": "10.0.0.6",
"foo3": "available"
},
{
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
"id": "xxx"
},
{
"network": "X.X.X.2",
"defaultGateway": "X.X.X.X",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.2",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.3",
"foo2": "network",
"foo3": "reserved",
"foo4": null,
"foo5": null,
"foo6": null,
"foo7": null,
"foo8": null,
"foo9": null,
"foo10": null,
"foo11": null
},
{
"foo1": "10.0.0.4",
"foo3": "available"
},
{
"foo1": "10.0.0.5",
"foo3": "available"
},
{
"foo1": "10.0.0.6",
"foo3": "available"
},
{
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
"id": "xxx"
}
]
我尝试的第一件事就是像这样显示每个元素:
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab}'
显示文件的每一行。
现在,我想显示一些行:
- 网络
- ipAddressTab
- foo1
- foo3
{
"network": "X.X.X.1",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo3": "reserved",
"foo1": "10.0.0.2",
"foo3": "reserved",
"foo1": "10.0.0.3",
"foo3": "reserved",
"foo1": "10.0.0.4",
"foo3": "available"
"foo1": "10.0.0.5",
"foo3": "available"
"foo1": "10.0.0.6",
"foo3": "available"
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
},
{
"network": "X.X.X.2",
"ipAddressTab": [
{
"foo1": "10.0.0.1",
"foo3": "reserved",
"foo1": "10.0.0.2",
"foo3": "reserved",
"foo1": "10.0.0.3",
"foo3": "reserved",
"foo1": "10.0.0.4",
"foo3": "available"
"foo1": "10.0.0.5",
"foo3": "available"
"foo1": "10.0.0.6",
"foo3": "available"
"foo1": "10.0.0.7",
"foo3": "available"
}
],
"full": false,
}
]
我试过类似的东西:
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab(.foo1, .foo3)}'
cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab[.foo1, .foo3]}'
但是没用!
谁能告诉我如何显示我需要的每个元素?
非常感谢!
首先select字段,然后修改ipAddressTab
。
map({network, ipAddressTab, full}
| .ipAddressTab[] |= {foo1, foo3})