使用JQ - 拆分和修改后连接记录

发布于 2025-01-09 18:09:36 字数 1313 浏览 1 评论 0原文

我有一个数组,其中一些记录需要重复。 id 值可能是 2 个字符串通过“;”连接,如下

{"_time": "2022-02-20T23","csp_name": "1","tool_bf_id": "1234", " id":"aa-1111;aa-2222"}

或 id 值为 null 或只有 1 个引用:

{"_time": "2022-02-20T23","csp_name": "3","tool_bf_id": "1357", "dvc_ssr": "null"}
{"_time": "2022-02-20T23","csp_name": "4","tool_bf_id": "2468", "dvc_ssr": "aa-3333"}

我能够复制此记录并且得到以下内容:

{"_time": "2022-02-20T23","csp_name": "1","tool_bf_id": "1234", "dvc_ssr": "aa-1111","id":"aa-1111;aa-2222"},
{"_time": "2022-02-20T23","csp_name": "1","tool_bf_id": "1234", "dvc_ssr": "aa-2222","id":"aa-1111;aa-2222"}

感谢另一篇文章: 使用公用密钥加入 2 个文件

我通过加入另一个文件中的数据来修改我的记录:

{"_time":"2022-02-20T23","csp_name":"1","tool_bf_id":"1234","id":"aa-1111;aa-2222", "dvc_ssr":"aa-1111","host":"hostId1"}
,{"_time":"2022-02-20T23","csp_name":"1","tool_bf_id":"1234","id":"aa-1111;aa-2222","dvc_ssr":"aa-2222","host":"hostId2"}

现在我需要合并记录返回并加入新添加字段(大约 10 个)中的值

{"_time": "2022-02-20T23","csp_name": "1","tool_bf_id": "1234", "id ":"aa-1111;aa-2222", "主机": "hostId1;hostId2"}

I have an array where some records need to be duplicated. The id value might be a join of 2 string by ";", as below

{"_time": "2022-02-20T23","csp_name": "1","tool_bf_id": "1234", "id":"aa-1111;aa-2222"}

or id value is null or only 1 ref:

{"_time": "2022-02-20T23","csp_name": "3","tool_bf_id": "1357", "dvc_ssr": "null"}
{"_time": "2022-02-20T23","csp_name": "4","tool_bf_id": "2468", "dvc_ssr": "aa-3333"}

I am able to duplicate this record and get the following:

{"_time": "2022-02-20T23","csp_name": "1","tool_bf_id": "1234", "dvc_ssr": "aa-1111","id":"aa-1111;aa-2222"},
{"_time": "2022-02-20T23","csp_name": "1","tool_bf_id": "1234", "dvc_ssr": "aa-2222","id":"aa-1111;aa-2222"}

Thanks to another post: Join 2 files using a common key

I modify my records by joining data from another file:

{"_time":"2022-02-20T23","csp_name":"1","tool_bf_id":"1234","id":"aa-1111;aa-2222", "dvc_ssr":"aa-1111","host":"hostId1"}
,{"_time":"2022-02-20T23","csp_name":"1","tool_bf_id":"1234","id":"aa-1111;aa-2222","dvc_ssr":"aa-2222","host":"hostId2"}

Now I need to merge the records back and join the value from the new added fields (which are around 10)

{"_time": "2022-02-20T23","csp_name": "1","tool_bf_id": "1234", "id":"aa-1111;aa-2222", "host": "hostId1;hostId2"}

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

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

发布评论

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

评论(1

姜生凉生 2025-01-16 18:09:36

hosts.jsonl

{ "host": "hostId1", "dvc_ssr": "aa-1111" }
{ "host": "hostId2", "dvc_ssr": "aa-2222" }
{ "host": "hostId3", "dvc_ssr": "aa-3333" }

records.jsonl

{ "_time": "2022-02-20T23", "csp_name": "1", "tool_bf_id": "1234", "id":"aa-1111;aa-2222" }
{ "_time": "2022-02-20T23", "csp_name": "3", "tool_bf_id": "1357", "dvc_ssr": "null" }
{ "_time": "2022-02-20T23", "csp_name": "4", "tool_bf_id": "2468", "dvc_ssr": "aa-3333" }
jq -n --slurpfile hosts hosts.jsonl '
   INDEX( $hosts[]; .dvc_ssr ) as $host_lkup |

   inputs |
   .host =
      if has("dvc_ssr") then
         $host_lkup[.dvc_ssr].host
      elif has("id") then
         .id / ";" | map( $host_lkup[.].host ) | join(";")
      else
         null
      end
' records.jsonl

输出:

{
  "_time": "2022-02-20T23",
  "csp_name": "1",
  "tool_bf_id": "1234",
  "id": "aa-1111;aa-2222",
  "host": "hostId1;hostId2"
}
{
  "_time": "2022-02-20T23",
  "csp_name": "3",
  "tool_bf_id": "1357",
  "dvc_ssr": "null",
  "host": null
}
{
  "_time": "2022-02-20T23",
  "csp_name": "4",
  "tool_bf_id": "2468",
  "dvc_ssr": "aa-3333",
  "host": "hostId3"
}

演示< /a> 在 jqplay 上

hosts.jsonl:

{ "host": "hostId1", "dvc_ssr": "aa-1111" }
{ "host": "hostId2", "dvc_ssr": "aa-2222" }
{ "host": "hostId3", "dvc_ssr": "aa-3333" }

records.jsonl:

{ "_time": "2022-02-20T23", "csp_name": "1", "tool_bf_id": "1234", "id":"aa-1111;aa-2222" }
{ "_time": "2022-02-20T23", "csp_name": "3", "tool_bf_id": "1357", "dvc_ssr": "null" }
{ "_time": "2022-02-20T23", "csp_name": "4", "tool_bf_id": "2468", "dvc_ssr": "aa-3333" }
jq -n --slurpfile hosts hosts.jsonl '
   INDEX( $hosts[]; .dvc_ssr ) as $host_lkup |

   inputs |
   .host =
      if has("dvc_ssr") then
         $host_lkup[.dvc_ssr].host
      elif has("id") then
         .id / ";" | map( $host_lkup[.].host ) | join(";")
      else
         null
      end
' records.jsonl

Output:

{
  "_time": "2022-02-20T23",
  "csp_name": "1",
  "tool_bf_id": "1234",
  "id": "aa-1111;aa-2222",
  "host": "hostId1;hostId2"
}
{
  "_time": "2022-02-20T23",
  "csp_name": "3",
  "tool_bf_id": "1357",
  "dvc_ssr": "null",
  "host": null
}
{
  "_time": "2022-02-20T23",
  "csp_name": "4",
  "tool_bf_id": "2468",
  "dvc_ssr": "aa-3333",
  "host": "hostId3"
}

Demo on jqplay

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