选择不同级别的多个字段 [英] Select multiple fields at different levels

查看:58
本文介绍了选择不同级别的多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.json文件,如下所示:

I've a .json file that look like :

[
    {
        "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"
    }
]

我尝试做的第一件事是显示每个这样的元素:

The first thing that I try that is to display each element like that :

cat myfile.txt | jq '.[]| {network: .network, ipAddressTab: .ipAddressTab}'

文件xas的每一行都显示.

Each lines of the file xas displayed.

现在,我想显示一些行:

Now, I want to display some lines :

  • 网络
  • 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]}'

但这是行不通的!

有人向我展示如何显示我需要的每个元素?

Some one to show me how to display each elements that I need ?

非常感谢!

推荐答案

首先选择字段,然后修改ipAddressTab.

First select the fields, then modify ipAddressTab.

map({network, ipAddressTab, full}
    | .ipAddressTab[] |= {foo1, foo3})

在线演示

这篇关于选择不同级别的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆