Linux JQ.如何从特定ID条目中提取数据

发布于 2025-01-12 04:40:16 字数 1280 浏览 0 评论 0原文

伙计,长期关注这个董事会并学到了很多东西,但现在遇到了一些问题。我正在使用读取 json 的 Linux shell 脚本(这里没问题)。我想做的是从具有特定类型的条目中获取值。

通过仅使用 jq -r '.' 解析 json,我得到

{
  "records": [
  {
    "id": 01,
    "type": "SOA",
    "name": "@",
    "data": "1800",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  },
  {
    "id": 02,
    "type": "A",
    "name": "@",
    "data": "test.com",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  }
  ],
  "links": {},
  "meta": {
    "total": 2
  }
}

然后,我使用 "jq -r '.records' " 并得到:

[
  {
    "id": 01,
    "type": "SOA",
    "name": "@",
    "data": "1800",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  },
  {
    "id": 02,
    "type": "A",
    "name": "@",
    "data": "test.com",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  }
]

我需要做什么是获取类型A的数据值。目前,我们有类型SOA和A,但我只需要从A获取数据。

我可以使用“jq -r '.records[1].data'的虚拟方式” 和它给了我 test.com 的正确响应,但我想要一种更动态的方式来搜索特定类型(在本例中为“A”),然后给出数据值。

谢谢你们!

Guy, long term looking at this board and learning a lot but now stuck with little issue. Im working with Linux shell script that reads json (no problem here). What Im trying to do is get value from entry that has specific Type.

By parsing a json with just jq -r '.', I get

{
  "records": [
  {
    "id": 01,
    "type": "SOA",
    "name": "@",
    "data": "1800",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  },
  {
    "id": 02,
    "type": "A",
    "name": "@",
    "data": "test.com",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  }
  ],
  "links": {},
  "meta": {
    "total": 2
  }
}

Then, I use "jq -r '.records' " and get:

[
  {
    "id": 01,
    "type": "SOA",
    "name": "@",
    "data": "1800",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  },
  {
    "id": 02,
    "type": "A",
    "name": "@",
    "data": "test.com",
    "priority": null,
    "port": null,
    "ttl": 1800,
    "weight": null,
    "flags": null,
    "tag": null
  }
]

What I need to do is get data value of the type A. Currently, we have type SOA and A, but I need to only get data from A.

I can use dummy way of "jq -r '.records[1].data'" and it gives me correct response of test.com, but I want a more dynamic way of searching for specific type (in this case "A") and then giving the data value.

Thanks guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼角的笑意。 2025-01-19 04:40:16

该字段名为 domain_records 还是只是 records

使用select来匹配您的条件

jq -r '.records[] | select(.type == "A").data'
test.com

演示

Is the field called domain_records or just records?

Use select to match your criteria

jq -r '.records[] | select(.type == "A").data'
test.com

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文