gpt4 book ai didi

json - 使用 jq 选择多个字段

转载 作者:行者123 更新时间:2023-12-03 21:27:06 25 4
gpt4 key购买 nike

我正在尝试使用以下 jq 过滤器过滤 docker-machine 的输出。

docker-machine inspect default | jq '{ConfigVersion, .Driver.{MachineName, CPU, Memory}, DriverName}'

第一个命令的原始 json 在这里
{
"ConfigVersion": 3,
"Driver": {
"IPAddress": "192.168.99.100",
"MachineName": "default",
"SSHUser": "docker",
"SSHPort": 52314,
"SSHKeyPath": "/Users/apatil/.docker/machine/machines/default/id_rsa",
"StorePath": "/Users/apatil/.docker/machine",
"SwarmMaster": false,
"SwarmHost": "tcp://0.0.0.0:3376",
"SwarmDiscovery": "",
"VBoxManager": {},
"HostInterfaces": {},
"CPU": 2,
"Memory": 5120,
"DiskSize": 20000,
"NatNicType": "82540EM",
"Boot2DockerURL": "",
"Boot2DockerImportVM": "",
"HostDNSResolver": false,
"HostOnlyCIDR": "192.168.99.1/24",
"HostOnlyNicType": "82540EM",
"HostOnlyPromiscMode": "deny",
"UIType": "headless",
"HostOnlyNoDHCP": false,
"NoShare": false,
"DNSProxy": true,
"NoVTXCheck": false,
"ShareFolder": ""
},
"DriverName": "virtualbox",
"HostOptions": {
"Driver": "",
"Memory": 0,
"Disk": 0,
"EngineOptions": {
"ArbitraryFlags": [],
"Dns": null,
"GraphDir": "",
"Env": [],
"Ipv6": false,
"InsecureRegistry": [],
"Labels": [],
"LogLevel": "",
"StorageDriver": "",
"SelinuxEnabled": false,
"TlsVerify": true,
"RegistryMirror": [],
"InstallURL": "https://get.docker.com"
},
"SwarmOptions": {
"IsSwarm": false,
"Address": "",
"Discovery": "",
"Agent": false,
"Master": false,
"Host": "tcp://0.0.0.0:3376",
"Image": "swarm:latest",
"Strategy": "spread",
"Heartbeat": 0,
"Overcommit": 0,
"ArbitraryFlags": [],
"ArbitraryJoinFlags": [],
"Env": null,
"IsExperimental": false
},
"AuthOptions": {
"CertDir": "/Users/apatil/.docker/machine/certs",
"CaCertPath": "/Users/apatil/.docker/machine/certs/ca.pem",
"CaPrivateKeyPath": "/Users/apatil/.docker/machine/certs/ca-key.pem",
"CaCertRemotePath": "",
"ServerCertPath": "/Users/apatil/.docker/machine/machines/default/server.pem",
"ServerKeyPath": "/Users/apatil/.docker/machine/machines/default/server-key.pem",
"ClientKeyPath": "/Users/apatil/.docker/machine/certs/key.pem",
"ServerCertRemotePath": "",
"ServerKeyRemotePath": "",
"ClientCertPath": "/Users/apatil/.docker/machine/certs/cert.pem",
"ServerCertSANs": [],
"StorePath": "/Users/apatil/.docker/machine/machines/default"
}
},
"Name": "default"
}

对于上述命令,我从 jq 收到以下错误
$ docker-machine inspect default | jq '{ConfigVersion, .Driver.{MachineName, CPU, Memory}, DriverName}'
jq: error: syntax error, unexpected FIELD (Unix shell quoting issues?) at <top-level>, line 1:
{ConfigVersion, .Driver.{MachineName, CPU, Memory}, DriverName}
jq: error: syntax error, unexpected '}', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
{ConfigVersion, .Driver.{MachineName, CPU, Memory}, DriverName}
jq: 2 compile errors

最佳答案

修复它使用

$ docker-machine inspect default |
jq '{ConfigVersion,
Driver: (.Driver|{MachineName, CPU, Memory}),
DriverName}'
{
"ConfigVersion": 3,
"Driver": {
"MachineName": "default",
"CPU": 2,
"Memory": 5120
},
"DriverName": "virtualbox"
}

关于json - 使用 jq 选择多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44786396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com