Json JQ if then语句范围

Json JQ if then语句范围,json,bash,jq,Json,Bash,Jq,我想使用JQ只获取与if-then语句匹配的子记录。当我使用 jq'if.services[].banner==“FQMDAAICCg==”then.services[].port else空端 它抓取所有端口以记录。(每个记录下都有多个服务,我想将then语句限制为我实际发现if条件的服务范围) 我如何在符合我条件的记录下获得特定服务的端口、横幅等 例如: { "services": [ { "tls_detected": false, "banner_is

我想使用JQ只获取与if-then语句匹配的子记录。当我使用

jq'if.services[].banner==“FQMDAAICCg==”then.services[].port else空端

它抓取所有端口以记录。(每个记录下都有多个服务,我想将then语句限制为我实际发现if条件的服务范围)

我如何在符合我条件的记录下获得特定服务的端口、横幅等

例如:

{
  "services": [
    {
      "tls_detected": false,
      "banner_is_raw": true,
      "transport_protocol": "tcp",
      "banner": "PCFET0NUWVBFIEhU",
      "certificate": null,
      "timestamp": "2020-03-22T00:38:01.074Z",
      "protocol": null,
      "port": 4444
    },
    {
      "tls_detected": false,
      "banner_is_raw": true,
      "transport_protocol": "tcp",
      "banner": "SFRUUC8xLjEgMzA",
      "certificate": null,
      "timestamp": "2020-03-19T01:39:45.288Z",
      "protocol": null,
      "port": 8080
    },
    {
      "tls_detected": false,
      "banner_is_raw": true,
      "transport_protocol": "tcp",
      "banner": "FQMDAAICCg==",
      "certificate": null,
      "timestamp": "2020-03-19T01:39:45.288Z",
      "protocol": null,
      "port": 8085
    },
    {
      "tls_detected": false,
      "banner_is_raw": false,
      "transport_protocol": "tcp",
      "banner": "Q2FjaGUtQ29ud",
      "certificate": null,
      "timestamp": "2020-03-20T04:25:24Z",
      "protocol": "http",
      "port": 8080
    }
  ],
  "ip": "103.238.62.68",
  "autonomous_system": {
    "description": "CHAPTECH-AS-AP Chaptech Pty Ltd",
    "asn": 133493,
    "routed_prefix": "103.238.62.0/24",
    "country_code": "AU",
    "name": "CHAPTECH-AS-AP Chaptech Pty Ltd",
    "path": [
      11164,
      3491,
      63956,
      7594,
      7594,
      7594,
      7594,
      133493
    ]
  },
  "location": {
    "country_code": "AU",
    "registered_country": "Australia",
    "registered_country_code": "AU",
    "continent": "Oceania",
    "timezone": "Australia/Sydney",
    "latitude": -33.494,
    "longitude": 143.2104,
    "country": "Australia"
  }
}
更新:

感谢peak,但我无法实现下面的额外目标。我最终使用了

jq 'select(.services[].banner == "FQMDAAICCg==") | {port: .services[].port, banner: .services[].banner, ip: .ip}' censys.json | jq 'if .banner == "FQMDAAICCg==" then .ip,.port else empty end'
这很难看,但却成功了,仍然允许我将数据流传输到第一个过滤器。

原始问题 我如何在符合我条件的记录下获得特定服务的端口、横幅等

要仅获取与条件匹配的服务的“端口”,您可以修改查询:

.services[]
| if .banner == "FQMDAAICCg==" then .port else empty end
相当于:

.services[]
| select(.banner == "FQMDAAICCg==")
| .port
附加目标 在本例中,我希望以“8085”+“103.238.62.68”结束

如果您真的希望这两个值采用该格式,那么可以按照以下行编写一些内容,使用-r选项调用jq:

.ip as $ip
| (.services[] | select(.banner == "FQMDAAICCg==") | .port) as $port
| "'\($port)' + '\($ip)'"
或者更简短但不太容易理解:

"'\(.services[] | select(.banner == "FQMDAAICCg==") | .port)' + '\(.ip)'"

第一部分非常有用,谢谢-我正在努力实现另一个目标:这就是您实际输入命令的方式吗?换行符是可选的。如果在为shell引用jq命令时遇到问题,我建议使用-f命令行选项。我尝试过:``jq-r'.ip作为$ip |服务[]如果.banner==“FQMDAAICCg==”那么.port作为$port else空结束'censys.json``和`jq.ip作为$ip |(.services[]| select(.banner==“FQMDAAICCg==”)|.port)作为$port'censys.json``都没有用。对不起,后者有什么明显的问题吗?你需要回顾一下jq的基本用法。最简单的方法是使用如下调用:
jq-rf PROGRAM.jq censys.json
@danielle…使用
#启动jq程序/usr/bin/jq-rf
shebang是一种获得这种效果的简单方法;然后shell引用就完全没有意义了,人们只能在文件中放入jq代码,按照自己的意愿进行格式化。