【问题标题】:jq - select all non-empty unique values of a key matching a regexjq - 选择与正则表达式匹配的键的所有非空唯一值
【发布时间】:2021-01-04 09:15:12
【问题描述】:

这是我的JSON 文件

[
    {
        "Body": [
            {
                "agentTechnologyType": "JAVA",
                "webApplicationId": "/solr",
                "webServerName": "localhost"
            },
           {
                "agentTechnologyType": "JAVA",
                "contextRoot": "/backoffice",
                "webApplicationId": "backoffice",
                "webServerName": "Catalina/localhost"
            },
            {
                "agentTechnologyType": "JAVA",
                "discoveredName": "ImpExImportJob",
                "displayName": "ImpExImportJob",
                "entityId": "SERVICE-D2B53C681D503292",
                "toRelationships": {
                    "calls": [
                        "SERVICE-A744C713BB5D832B"
                    ]
                }
            },
            {
                "agentTechnologyType": "JAVA",
                "discoveredName": "CronJobs"
            },
            {
                "agentTechnologyType": "APACHE",
                "contextRoot": "/",
                "discoveredName": "www.example.com:443",
                "displayName": "www.example.com:443",
                "serviceType": "WebRequest",
                "webServerName": "www.example.com:443"
            },
            {
                "agentTechnologyType": "APACHE",
                "contextRoot": "/service",
                "discoveredName": "www.example.com:443",
                "displayName": "www.example.com:443",
                "entityId": "SERVICE-96405085FB29CC50",
                "webServerName": "www.example.com:443"
            },
            {
                "agentTechnologyType": "APACHE",
                "contextRoot": "/nodes",
                "discoveredName": "www.exampleA.com:443",
                "displayName": "www.exampleA.com:443",
                "entityId": "SERVICE-15578BCF4A5FF4D5",
                "toRelationships": {},
                "webServerName": "www.exampleA.com:443"
            },
            {
                "agentTechnologyType": "APACHE",
                "contextRoot": "/",
                "discoveredName": "www.example.com:80",
                "displayName": "www.example.com:80",
                "entityId": "SERVICE-F9193C47A131E506",
                "toRelationships": {},
                "webServerName": "www.example.com:80"
            }
        ]
    }
]

我的任务 - 选择具有 Web URL 形式的 "webServerName" 键的所有 唯一、非空 值,以 :443 结尾,并在不带双引号的情况下显示所有这些值每一行。

结果应如下所示:

www.example.com:443
www.exampleA.com:443

请注意,"webServerName" 不存在于某些对象中,并且也可能具有重复值。

我想出了以下 jq 过滤器

jq -r '[.[] |.Body | .[]  | select((.webServerName !=null) and (.webServerName | test ("\\w[a-z0-9-.]+.[a-z]{2,3}:443$")) ) ] | [.[].webServerName] | unique | .[]' services.json

我的问题 - 有没有更优雅、更简单的过滤器,jq 也能做到这一点?

附:一项附加要求 - 排除在 url 中具有特定模式的值。 例如urlurl 中包含server-internal,那么www.server-internal.example.com:443server-internal.b.example.com:443 应该被过滤掉。 以上是用正则表达式更好还是有更好的方法?

【问题讨论】:

  • 我可以更新我的答案,但请记住一次性将您的所有要求发布到问题中
  • 谢谢!是的,我应该更详细地说明我为什么使用正则表达式。
  • 再次感谢,非常漂亮和优雅。如果需要,我可以使用表达式test (a|b) | not 添加更多排除条件

标签: arrays json search jq


【解决方案1】:

您可以非常简单地在 webServerName 字段上使用递归下降,忽略空字段并使用 endswith/1 函数选择以 :443 结尾的字段,这比使用基于正则表达式的方法简单得多。

在获得的结果上,使用unique 函数删除重复的条目。

[ .. | .webServerName? | select(. != null and endswith(":443")) ] | unique[]

在命令行中使用-r 标志以原始输出模式发出输出以松开引号。

另一个涉及map的变体,自上而下遍历

map(.Body[].webServerName | select(. != null and endswith(":443") ) ) | unique[]

OP 有一个后续要求,即忽略包含 "server-internal" 部分的 URL,您可以为其添加另一个条件到选择中

map( .Body[].webServerName | 
  select( ( . != null and endswith(":443") ) and 
    ( test("server-internal") | not ) ) ) | 
unique[]

【讨论】:

  • 太棒了!非常感谢。只有一个相关的问题 - 如果我想添加一个附加条件,不仅值以 ":443" 结尾,而且不包含像 my-hosting 这样的模式,所以 URL www.internal.my-hosting-a.example.com:443" 不包含在结果中 - 将 @987654334 @ 或 contains 工作,还是更好地使用正则表达式?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多