本文作者:念宗

shell读取json格式输出

念宗 6年前 ( 2018-12-21 ) 5274 抢沙发
摘要: 举个json输出的例子,以docker inspect的输出为例,当然docker inspect -f有自己的处理方式,这里介绍通过shell命令来获取键值的方法:[root@n...

举个json输出的例子,以docker inspect的输出为例,当然docker inspect -f有自己的处理方式,这里介绍通过shell命令来获取键值的方法:

[root@node2 ~]# docker inspect 407f5d84227a|awk -F'[,]+' '{for(i=1;i<=NF;i++)if($i~"Pid")print $i}'|awk '/\<Pid\>/{print $2}'
3744

json输出为下面的格式:

[root@node2 ~]# docker inspect 407f5d84227a
[
    {
        "Id": "407f5d84227a6731287dcc0396f48dbd96a6f003c263b89c965515a09f628330",
        "Created": "2018-12-20T10:25:08.960267879Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 3744,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-12-20T10:31:33.593101329Z",
            "FinishedAt": "2018-12-20T10:26:25.503128054Z"
        },
        "Image": "sha256:e54faac158ffc33e92a3fe1f59ccc0b8bbe375cf1b672537e2fbd2b7606beeb5",
        "ResolvConfPath": "/var/lib/docker/containers/407f5d84227a6731287dcc0396f48dbd96a6f003c263b89c965515a09f628330/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/407f5d84227a6731287dcc0396f48dbd96a6f003c263b89c965515a09f628330/hostname",
        "HostsPath": "/var/lib/docker/containers/407f5d84227a6731287dcc0396f48dbd96a6f003c263b89c965515a09f628330/hosts",
        "LogPath": "/var/lib/docker/containers/407f5d84227a6731287dcc0396f48dbd96a6f003c263b89c965515a09f628330/407f5d84227a6731287dcc0396f48dbd96a6f003c263b89c965515a09f628330-json.log",
        "Name": "/centos6.8",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/d23869cc943bb081239f7d31e097fb60001b7a8777361819d7c78d7f9b9addb7-init/diff:/var/lib/docker/overlay2/43f531f1fe78a700d61d98306bad92dd005a7d135a1f691a66446086c3f4b2d3/diff",
                "MergedDir": "/var/lib/docker/overlay2/d23869cc943bb081239f7d31e097fb60001b7a8777361819d7c78d7f9b9addb7/merged",
                "UpperDir": "/var/lib/docker/overlay2/d23869cc943bb081239f7d31e097fb60001b7a8777361819d7c78d7f9b9addb7/diff",
                "WorkDir": "/var/lib/docker/overlay2/d23869cc943bb081239f7d31e097fb60001b7a8777361819d7c78d7f9b9addb7/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "407f5d84227a",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "e54faac158ff",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "build-date": "2016-06-02",
                "license": "GPLv2",
                "name": "CentOS Base Image",
                "vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "36b152017920b7719f89fd6618755d5aa8e807eb0689fa46c527d48a6f9a34df",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/36b152017920",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "c160432b9050c7d1b800e8b615ee94e9e4f5f7c2075d1642d827e0406510a177",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "f75daaca64110e0f764c30ef1357d0db0d55c05f0559880bf10467d06c293c37",
                    "EndpointID": "c160432b9050c7d1b800e8b615ee94e9e4f5f7c2075d1642d827e0406510a177",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]        "HostnamePath": "/var/lib/docker/containers/407f5d84227a6731287dcc0396f48dbd96a6f003c263b89c965515a09f628330/hostname",


文章版权及转载声明:

作者:念宗本文地址:http://pyops.net/?id=46发布于 6年前 ( 2018-12-21 )
文章转载或复制请以超链接形式并注明出处运维之道

分享到: 网站分享代码

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

发表评论

快捷回复:

验证码

评论列表 (暂无评论,5274人围观)参与讨论

还没有评论,来说两句吧...