简体   繁体   English

如何使用 jq 命令将数据永久添加到 json 文件

[英]How to permanently add data to json file using jq command

Below is my taskdef.json file下面是我的 taskdef.json 文件

{
    "containerDefinitions": [
        {
            "name": "container_name",
            "image": "",
            "essential": true,
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "/ecs/taskdef",
                    "awslogs-region": "us-east-2",
                    "awslogs-stream-prefix": "ecs"
                }
            }
         }
     ]
  }

I'm using an environment variable to append to this file我正在使用环境变量附加到这个文件

export IMAGE=########.dkr.ecr.us-west-1.amazonaws.com/container-repo:latest

I've ran the following command to input the image under containerDefinitions and the output shows it in there but it does not get appended permanently to the taskdef.json file.我已运行以下命令在 containerDefinitions 下输入图像,输出将其显示在其中,但它不会永久附加到 taskdef.json 文件中。

jq ".containerDefinitions[].image=\"$IMAGE\"" taskdef.json

I've also tried the following command to add the output to a new file but I have multiple json data injections so this way doesn't work.我还尝试了以下命令将输出添加到新文件,但我有多个 json 数据注入,所以这种方式不起作用。

jq ".containerDefinitions[].image=\"$IMAGE\"" taskdef.json > taskdef2.json

Is there anyway to inject an input permanently into the json file using the jq command which would be equivalent to sed -i?无论如何使用相当于 sed -i 的 jq 命令将输入​​永久注入到 json 文件中?

jq does not have a command-line option like sed's -i . jq 没有像 sed 的-i这样的命令行选项。 One way to avoid having to fiddle with a temporary file is to use sponge (part of the moreutils collection) if it is available in your computing environment:如果它在您的计算环境中可用,则避免不得不摆弄临时文件的一种方法是使用spongemoreutils集合的一部分):

jq .... taskdef.json | sponge taskdef.json

(Homebrew users can install sponge via: brew install sponge ) (自制用户可以通过以下方式安装spongebrew install sponge

For a PowerShell alternative to sponge , see Powershell equivalent to sponge in moreutils?有关sponge的 PowerShell 替代品,请参阅在 moreutils? 中等同于海绵的 Powershell?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM