jq在外面添加捕获组结果 [英] jq add capturing group result outside

查看:91
本文介绍了jq在外面添加捕获组结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,

输入:

    {
       "id":"abc",
       "name": "name-middlenane-lastname-1"
    },

    {
        "id":"123",
        "name": "fname-flast-2"
    }

响应:

    {
         "id":"abc",
         "name": "name-middlename-lastname-1",
         "newkey": "name-middlename-lastname"
    },

    {
          "id":"123",
          "name": "fname-flast-2",
          "newkey": "fname-flast"
    }

每个对象中归档的名称是一个字符串,其字符和数字之间用-" 连字符分隔.我需要从头到尾的完整字符串.我不想要号码后面的任何东西.然后,应提取键为 newkey 的添加 new 字段,并提取不包含数字的字符串.因此,输出应包含旧字段和新字段.

The filed name in each object is a string with characters and numbers separated by "-" hyphen. I need the complete string from beginning till the starting number. I don't want anything which is there after the number. And then the add new field with key as newkey and value should be extracted string without the number. Thus, the output should contain old fields as well as new one.

推荐答案

jq 解决方案:

jq solution:

示例input.json:

[
  {
    "id": "abc",
    "name": "name-middlenane-lastname-1"
  },
  {
    "id": "123",
    "name": "fname-flast-2"
  }
]


jq 'map(. + (.name | capture("(?<newkey>.+)-[0-9]+")) )' input.json

输出:

[
  {
    "id": "abc",
    "name": "name-middlenane-lastname-1",
    "newkey": "name-middlenane-lastname"
  },
  {
    "id": "123",
    "name": "fname-flast-2",
    "newkey": "fname-flast"
  }
]

这篇关于jq在外面添加捕获组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆