bash中的解析结果

bash中的解析结果,bash,grep,Bash,Grep,我有两个查询,它们给出了结果: 服务 服务数量 例如: root@:~/elo# cat test | grep name | grep -v expand | cut -c 22- | rev | cut -c 3- | rev service1 service2 root@:~/elo# cat test | grep customfield | cut -c 31- | rev | cut -c 2- | rev 2.3.4 55.66 我想将第一次查询的第一个值与第二次查询的第一

我有两个查询,它们给出了结果:

  • 服务
  • 服务数量
  • 例如:

    root@:~/elo# cat test  | grep name | grep -v expand | cut -c 22- | rev | cut -c 3- | rev
    service1
    service2
    root@:~/elo# cat test  | grep customfield |  cut -c 31- | rev | cut -c 2- | rev
    2.3.4
    55.66
    
    我想将第一次查询的第一个值与第二次查询的第一个值连接起来。在本例中,应为:

    service1:2.3.4
    service2:55.66
    

    您可以使用
    粘贴

    paste -d: <(grep name test| grep -v expand | cut -c 22- | rev | cut -c 3- | rev) \
              <(grep customfield test |  cut -c 31- | rev | cut -c 2- | rev)
    

    paste-d:没有示例文件,很难编写一个工作示例。但我明白了,这两个值都来自同一个文本文件和同一行。因此,我将使用awk来实现:

    $ cat text
    service1;some_other_text;2.3.4
    service2;just_some_text;55.66
    
    $awk -F ";" '{printf "%s:%s\n",  $1, $3}' test 
    service1:2.3.4
    
    对于JSON文件,如果可以使用jg(例如apt get install jg),则更容易:


    sed是删除引号所必需的。

    您可以为此编写一个bash脚本。@rafal1337,请在您的帖子中发布您的示例输入文件,然后让我们知道吗?@RavinderSingh13这是json文件。我将他解析为我的预期输出。
    {…“name”:“service1…”“customfield_10090”:“1.2.3”},…“name”:“service2”}…],“customfield_10090”:“23.3.2”
    这是缩写的json文件。在我的情况下,不工作-
    jq:error(在测试中:42):无法使用字符串“customfield_10090”为字符串编制索引。
    这是我的整个json文件:您必须调整所需参数的路径:
    jq'。问题[]。字段|。组件[]。名称+:“+.customfield_10090”测试。json
    $ cat test.json
    [
      {
        "name": "service1",
        "customfield_10090": "1.2.3"
      },
      {
        "name": "service2",
        "customfield_10090": "23.3.2"
      }
    ]
    
    $jq '.[] | .name + ":" + .customfield_10090' test.json | sed 's/"//g'
    service1:1.2.3
    service2:23.3.2