【发布时间】: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 中具有特定模式的值。
例如url 在url 中包含server-internal,那么www.server-internal.example.com:443 或server-internal.b.example.com:443 应该被过滤掉。
以上是用正则表达式更好还是有更好的方法?
【问题讨论】:
-
我可以更新我的答案,但请记住一次性将您的所有要求发布到问题中
-
谢谢!是的,我应该更详细地说明我为什么使用正则表达式。
-
再次感谢,非常漂亮和优雅。如果需要,我可以使用表达式
test (a|b) | not添加更多排除条件