简体   繁体   English

如何使用脚本外壳将文件附加到具有合适格式的另一个文件中

[英]how can i append a file into another file with a suitable format using script shell

I have this json file template.json我有这个 json 文件 template.json

{
    "PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
    "PTF_CONTENT": [
      {
        "NAME": "api_batch_API",
        "CHECKED": "api_batch_NOT_SELECTED",
        "VERSION": "G02R00C13_c14"
      }
    ]
  }

And this file historique.json,It created by script bash , I call the script in Jenkinsfile而这个文件 historique.json,它是由脚本 bash 创建的,我在 Jenkinsfile 中调用该脚本

  [
   {
        "PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
        "PTF_CONTENT": [
          {
            "NAME": "api_batch_API",
            "CHECKED": "api_batch_NOT_SELECTED",
            "VERSION": "G02R00C13_c14"
          }
        ]
     }
   ]

So when I do a build in jenkins an element has added in the last of file historique.json like below:因此,当我在 jenkins 中进行构建时,在 historique.json 文件的最后一个添加了一个元素,如下所示:

[
    {
           "PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
           "PTF_CONTENT": [
             {
                "NAME": "api_batch_API",
                "CHECKED": "api_batch_NOT_SELECTED",
                "VERSION": "G02R00C13_c14"
              }
            ]
         }
]
{
        "PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
        "PTF_CONTENT": [
          {
            "NAME": "api_batch_API",
            "CHECKED": "api_batch_NOT_SELECTED",
            "VERSION": "G02R00C13_c14"
          }
        ]
  }

And ect.等等。 So my question is how can I formatted the historique file to be like below:所以我的问题是如何将历史文件格式化为如下所示:

 [
      {
           "PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
           "PTF_CONTENT": [
                  {
                    "NAME": "api_batch_API",
                    "CHECKED": "api_batch_NOT_SELECTED",
                    "VERSION": "G02R00C13_c14"
                  }
        ]
     },

    {
            "PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
            "PTF_CONTENT": [
              {
                "NAME": "api_batch_API",
                "CHECKED": "api_batch_NOT_SELECTED",
                "VERSION": "G02R00C13_c14"
              }
       ]
    }
]

This my script script.sh这是我的脚本 script.sh

#!/bin/bash

a=$(cat template.json)

if [ -e $WORKSPACE/project/historique.json ];
then
  cat template.json >> historique.json
else
  cat <<-EOF > historique.json
   [
    $a
   ]
EOF
fi

PS: The file template.json change at each build in jenkins PS:文件 template.json 在 jenkins 中每次构建时都会更改

The problem is the command after "then".问题是“then”之后的命令。 Any suggestion please??(And sorry for my English)请问有什么建议吗??(对不起我的英语)

After oguz ismail and shawn response the script file become like that:在 oguz ismail 和 shawn 响应之后,脚本文件变成这样:

#!/bin/bash

a=$(cat template.json)

if [ -e $WORKSPACE/project-CRMGP/historique.json ];
then
  jq '. + [input]' historique.json template.json > historique1.json 
else
  cat <<-EOF > historique.json
   [
    $a
   ]
EOF
fi

mv historique1.json historique.json

This my script script.sh这是我的脚本 script.sh

Please use a decicated tool to process JSON.请使用专用工具来处理 JSON。 With xidel for instance you don't even need the 'script.sh' at all:例如,使用xidel ,您甚至根本不需要“script.sh”

$ xidel -s template.json -e '
  if (file:exists("'$WORKSPACE/project-CRMGP/historique.json'")) then
    array{
      json-doc("'$WORKSPACE/project-CRMGP/historique.json'")(),
      $json
    }
  else
    array{$json}
'
[
  {
    "PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
    "PTF_CONTENT": [
      {
        "NAME": "api_batch_API",
        "CHECKED": "api_batch_NOT_SELECTED",
        "VERSION": "G02R00C13_c14"
      }
    ]
  },
  {
    "PTF_INSTALL_DATE": " 2020-03-31 09:12:10",
    "PTF_CONTENT": [
      {
        "NAME": "api_batch_API",
        "CHECKED": "api_batch_NOT_SELECTED",
        "VERSION": "G02R00C13_c14"
      }
    ]
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用脚本外壳获取 json 文件的 n 个元素 - how can i got n element of json file using script shell 如何解析 JSON 文件中的数据并将其保存到 shell 脚本中的另一个文本文件 - How do I parse data from a JSON file and save it to another text file in shell script 如何在 JSON 文件中附加字典,同时保持正确的 JSON 格式 - How can I append a dictionary in a JSON file, whilst maintaining correct JSON format 如何使用 shell 脚本获取 json 文件的值? - How to fetch values of json file using shell script? 如何使用shell脚本将json对象添加到json文件 - how to add json object to json file using shell script 如何使用 shell 脚本将 json 对象从文件上传到 couchdb? - How to upload json objects from a file to couchdb using a shell script? 什么是良好的Python配置文件格式,可以通过一个脚本轻松进行编辑,而又可以轻松又安全地读取另一个脚本? - What is a good Python configuration file format that can be edited by one script easily and read by another script easily and safely? 使用shell脚本创建嵌套的json文件 - Create nested json file using shell script 使用Shell脚本将JSON文件发布到URL - Post a JSON file to a URL using Shell Script 我如何在 for 循环中 append 对 json 文件的字典? - How can i append a dict to a json file in a for loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM