jq:仅当字段 n 等于 x 时为必填字段
jq: only required fields if the field n is equal to x
我有以下文件:
[
{
"code": 200,
"status": "OK",
"result": {
"query": "123",
"hits": [
{
"ip": "2.14.11.41",
"services": [
{
"port": 22,
"service_name": "SSH",
"transport_protocol": "TCP"
},
{
"port": 80,
"service_name": "HTTP",
"transport_protocol": "TCP"
},
{
"port": 5900,
"service_name": "VNC",
"transport_protocol": "TCP"
},
{
"port": 8001,
"service_name": "HTTP",
"certificate": "62345a087218e",
"transport_protocol": "TCP"
}
],
"location": {
"continent": "Asia",
"country": "Israel",
"country_code": "IL",
"timezone": "Asia/Jerusalem",
"coordinates": {
"latitude": 31.5,
"longitude": 34.75
},
"registered_country": "Israel",
"registered_country_code": "IL"
},
"autonomous_system": {
"asn": 12400,
"description": "PARTNER-AS",
"bgp_prefix": "2.54.0.0/17",
"name": "PARTNER-AS",
"country_code": "IL"
},
"last_updated_at": "2022-05-18T13:09:19.288Z"
},
{
"ip": "2.22.22.102",
"services": [
{
"port": 22,
"service_name": "SSH",
"transport_protocol": "TCP"
},
{
"port": 80,
"service_name": "HTTP",
"transport_protocol": "TCP"
},
{
"port": 5901,
"service_name": "VNC",
"transport_protocol": "TCP"
},
{
"port": 8001,
"service_name": "HTTP",
"certificate": "6875897649b700a087218e",
"transport_protocol": "TCP"
},
{
"port": 8089,
"service_name": "HTTP",
"transport_protocol": "TCP"
}
],
"location": {
"continent": "Asia",
"country": "Israel",
"country_code": "IL",
"city": "Herzliya",
"timezone": "Asia/Jerusalem",
"province": "Tel Aviv",
"coordinates": {
"latitude": 32.1679,
"longitude": 34.834
},
"registered_country": "Israel",
"registered_country_code": "IL"
},
"autonomous_system": {
"asn": 12400,
"description": "PARTNER-AS",
"bgp_prefix": "2.55.0.0/17",
"name": "PARTNER-AS",
"country_code": "IL"
},
"last_updated_at": "2022-05-18T13:50:58.807Z"
},
{
"ip": "4.54.84.19",
"services": [
{
"port": 22,
"service_name": "SSH",
"transport_protocol": "TCP"
},
{
"port": 443,
"service_name": "HTTP",
"certificate": "8120978819a83e",
"transport_protocol": "TCP"
},
{
"port": 502,
"service_name": "MODBUS",
"transport_protocol": "TCP"
},
{
"port": 5900,
"service_name": "VNC",
"transport_protocol": "TCP"
}
],
"location": {
"continent": "Asia",
"country": "Israel",
"country_code": "IL",
"city": "Ashdod",
"timezone": "Asia/Jerusalem",
"province": "Southern District",
"coordinates": {
"latitude": 31.7915,
"longitude": 34.6497
},
"registered_country": "Israel",
"registered_country_code": "IL"
},
"autonomous_system": {
"asn": 12400,
"description": "PARTNER-AS",
"bgp_prefix": "2.55.0.0/17",
"name": "PARTNER-AS",
"country_code": "IL"
},
"last_updated_at": "2022-05-18T12:36:13.141Z"
}
],
"links": {
"next": "eyJ9I6MH0=",
"prev": ""
}
}
}
]
我正在尝试从中获取以下格式:
2.14.11.41:80
2.14.11.41:8001
2.22.22.102:80
2.22.22.102:8001
2.22.22.102:8089
4.54.84.19:443
即select IP 和端口,如果 service_name 等于 HTTP。我知道如何获取 IP jq -r '.[].result.hits[] | select(.services[].service_name == "HTTP") | .ip'
以及如何仅获取端口 jq -r '.[].result.hits[].services[] | select(.service_name == "HTTP") | .port'
,但我不明白如何在一个命令中正确组合它。有人可以指出正确的方向吗?
将 IP 保存在变量中,使您的选择更深一层。然后,如果匹配则打印出来:
jq -r '.[].result.hits[] | .ip as $ip | .services[] | select(.service_name == "HTTP") | "\($ip):\(.port)"'
2.14.11.41:80
2.14.11.41:8001
2.22.22.102:80
2.22.22.102:8001
2.22.22.102:8089
4.54.84.19:443
我有以下文件:
[
{
"code": 200,
"status": "OK",
"result": {
"query": "123",
"hits": [
{
"ip": "2.14.11.41",
"services": [
{
"port": 22,
"service_name": "SSH",
"transport_protocol": "TCP"
},
{
"port": 80,
"service_name": "HTTP",
"transport_protocol": "TCP"
},
{
"port": 5900,
"service_name": "VNC",
"transport_protocol": "TCP"
},
{
"port": 8001,
"service_name": "HTTP",
"certificate": "62345a087218e",
"transport_protocol": "TCP"
}
],
"location": {
"continent": "Asia",
"country": "Israel",
"country_code": "IL",
"timezone": "Asia/Jerusalem",
"coordinates": {
"latitude": 31.5,
"longitude": 34.75
},
"registered_country": "Israel",
"registered_country_code": "IL"
},
"autonomous_system": {
"asn": 12400,
"description": "PARTNER-AS",
"bgp_prefix": "2.54.0.0/17",
"name": "PARTNER-AS",
"country_code": "IL"
},
"last_updated_at": "2022-05-18T13:09:19.288Z"
},
{
"ip": "2.22.22.102",
"services": [
{
"port": 22,
"service_name": "SSH",
"transport_protocol": "TCP"
},
{
"port": 80,
"service_name": "HTTP",
"transport_protocol": "TCP"
},
{
"port": 5901,
"service_name": "VNC",
"transport_protocol": "TCP"
},
{
"port": 8001,
"service_name": "HTTP",
"certificate": "6875897649b700a087218e",
"transport_protocol": "TCP"
},
{
"port": 8089,
"service_name": "HTTP",
"transport_protocol": "TCP"
}
],
"location": {
"continent": "Asia",
"country": "Israel",
"country_code": "IL",
"city": "Herzliya",
"timezone": "Asia/Jerusalem",
"province": "Tel Aviv",
"coordinates": {
"latitude": 32.1679,
"longitude": 34.834
},
"registered_country": "Israel",
"registered_country_code": "IL"
},
"autonomous_system": {
"asn": 12400,
"description": "PARTNER-AS",
"bgp_prefix": "2.55.0.0/17",
"name": "PARTNER-AS",
"country_code": "IL"
},
"last_updated_at": "2022-05-18T13:50:58.807Z"
},
{
"ip": "4.54.84.19",
"services": [
{
"port": 22,
"service_name": "SSH",
"transport_protocol": "TCP"
},
{
"port": 443,
"service_name": "HTTP",
"certificate": "8120978819a83e",
"transport_protocol": "TCP"
},
{
"port": 502,
"service_name": "MODBUS",
"transport_protocol": "TCP"
},
{
"port": 5900,
"service_name": "VNC",
"transport_protocol": "TCP"
}
],
"location": {
"continent": "Asia",
"country": "Israel",
"country_code": "IL",
"city": "Ashdod",
"timezone": "Asia/Jerusalem",
"province": "Southern District",
"coordinates": {
"latitude": 31.7915,
"longitude": 34.6497
},
"registered_country": "Israel",
"registered_country_code": "IL"
},
"autonomous_system": {
"asn": 12400,
"description": "PARTNER-AS",
"bgp_prefix": "2.55.0.0/17",
"name": "PARTNER-AS",
"country_code": "IL"
},
"last_updated_at": "2022-05-18T12:36:13.141Z"
}
],
"links": {
"next": "eyJ9I6MH0=",
"prev": ""
}
}
}
]
我正在尝试从中获取以下格式:
2.14.11.41:80
2.14.11.41:8001
2.22.22.102:80
2.22.22.102:8001
2.22.22.102:8089
4.54.84.19:443
即select IP 和端口,如果 service_name 等于 HTTP。我知道如何获取 IP jq -r '.[].result.hits[] | select(.services[].service_name == "HTTP") | .ip'
以及如何仅获取端口 jq -r '.[].result.hits[].services[] | select(.service_name == "HTTP") | .port'
,但我不明白如何在一个命令中正确组合它。有人可以指出正确的方向吗?
将 IP 保存在变量中,使您的选择更深一层。然后,如果匹配则打印出来:
jq -r '.[].result.hits[] | .ip as $ip | .services[] | select(.service_name == "HTTP") | "\($ip):\(.port)"'
2.14.11.41:80
2.14.11.41:8001
2.22.22.102:80
2.22.22.102:8001
2.22.22.102:8089
4.54.84.19:443