由于blog各种垃圾评论太多,而且本人审核评论周期较长,所以懒得管理评论了,就把评论功能关闭,有问题可以直接qq骚扰我

bash格式化输出python

Linux 西门飞冰 5170℃

我们在日常工作中,有时候需要访问一些API接口,这些接口都是以json格式返回的,但是在shell中,默认对json显示不是很友好,如下所示:

[root@c7-node1 ~]# curl -s http://192.168.28.71:9200/_tasks
{"nodes":{"iJIboMLLRwWwai1WWSXFMg":{"name":"iJIboML","transport_address":"192.168.28.71:9300","host":"192.168.28.71","ip":"192.168.28.71:9300","roles":["master","data","ingest"],"tasks":{"iJIboMLLRwWwai1WWSXFMg:42373":{"node":"iJIboMLLRwWwai1WWSXFMg","id":42373,"type":"transport","action":"cluster:monitor/tasks/lists","start_time_in_millis":1520940440161,"running_time_in_nanos":956890,"cancellable":false},"iJIboMLLRwWwai1WWSXFMg:42374":{"node":"iJIboMLLRwWwai1WWSXFMg","id":42374,"type":"direct","action":"cluster:monitor/tasks/lists[n]","start_time_in_millis":1520940440162,"running_time_in_nanos":297574,"cancellable":false,"parent_task_id":"iJIboMLLRwWwai1WWSXFMg:42373"}}}}}

如果我们想要在bash中输出json格式,可以将URL的返回结果,传给python来进行解析,如下所示:

[root@c7-node1 ~]# curl -s http://192.168.28.71:9200/_tasks | python -mjson.tool
{
    "nodes": {
        "iJIboMLLRwWwai1WWSXFMg": {
            "host": "192.168.28.71",
            "ip": "192.168.28.71:9300",
            "name": "iJIboML",
            "roles": [
                "master",
                "data",
                "ingest"
            ],
            "tasks": {
                "iJIboMLLRwWwai1WWSXFMg:42379": {
                    "action": "cluster:monitor/tasks/lists",
                    "cancellable": false,
                    "id": 42379,
                    "node": "iJIboMLLRwWwai1WWSXFMg",
                    "running_time_in_nanos": 328807,
                    "start_time_in_millis": 1520940460219,
                    "type": "transport"
                },
                "iJIboMLLRwWwai1WWSXFMg:42380": {
                    "action": "cluster:monitor/tasks/lists[n]",
                    "cancellable": false,
                    "id": 42380,
                    "node": "iJIboMLLRwWwai1WWSXFMg",
                    "parent_task_id": "iJIboMLLRwWwai1WWSXFMg:42379",
                    "running_time_in_nanos": 40144,
                    "start_time_in_millis": 1520940460219,
                    "type": "direct"
                }
            },
            "transport_address": "192.168.28.71:9300"
        }
    }
}

转载请注明:西门飞冰的博客 » bash格式化输出python

喜欢 (0)or分享 (0)