使用重定向运算符通过jq更新文件内容

编程入门 行业动态 更新时间:2024-10-13 20:20:09
本文介绍了使用重定向运算符通过jq更新文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的JSON输入文件如下:

{ "Name":"SA", "Password":"yyyyy", "Mappings" : { "RegionMap" : { "us-east-1" : { "AMI" : "xxxxxx" }, "us-east-2" : { "AMI" : "" }, "us-west-1" : { "AMI" : "" }, "us-west-2" : { "AMI" : "" }, "ca-central-1" : { "AMI" : "" }, "eu-central-1" : { "AMI" : "" }, "eu-west-1" : { "AMI" : "" }, "eu-west-2" : { "AMI" : "" }, "ap-south-1" : { "AMI" : "" }, "ap-southeast-1" : { "AMI" : "" }, "ap-southeast-2" : { "AMI" : "" }, "ap-northeast-1" : { "AMI" : "" }, "ap-northeast-2" : { "AMI" : "" }, "sa-east-1" : { "AMI" : "" } } } }

我的任务是更新给定json文件中us-east-1的AMI键的值,并将更新后的文件创建为新文件.我正在使用带有重定向运算符的 jq 插件.它具有 + 运算符,可用于overwrite对象中的内容.

My task is to update the value of the AMI key of us-east-1 in the given json file and create the updated file as new file. I am using the jq plugin with redirection operator. It has + operator which can used to overwrite contents in an object.

我的expected输出是

{ "Name":"SA", "Password":"yyyyy", "Mappings" : { "RegionMap" : { "us-east-1" : { "AMI" : "abcd" }, "us-east-2" : { "AMI" : "" }, "us-west-1" : { "AMI" : "" }, "us-west-2" : { "AMI" : "" }, "ca-central-1" : { "AMI" : "" }, "eu-central-1" : { "AMI" : "" }, "eu-west-1" : { "AMI" : "" }, "eu-west-2" : { "AMI" : "" }, "ap-south-1" : { "AMI" : "" }, "ap-southeast-1" : { "AMI" : "" }, "ap-southeast-2" : { "AMI" : "" }, "ap-northeast-1" : { "AMI" : "" }, "ap-northeast-2" : { "AMI" : "" }, "sa-east-1" : { "AMI" : "" } } } }

我当前的命令未提供预期的输出,其结果如下:

My current command is not providing the expected output and its as follows:

jq '.Mappings.RegionMap + { "us-east-1":{"AMI":"abcd"}}' <OldfileName> > <Newfilename>

有人可以帮助我使用重定向运算符实现预期的输出吗?

Can somebody help me with achieving the expected output using redirection operator ?

我在cygwin环境上运行命令,而我的 jq 版本是.

I am running the command on a cygwin environment and my jq version is 1.5 .

编辑

在密码密钥后添加了逗号

Added a commas after the Password Key

推荐答案

使输入json无效的第一个问题是此行(第三行):

The first issue that makes your input json invalid is this line (the 3rd line):

... "Password":"yyyyy" <---- ...

应紧跟,.

修复此问题后,您可以轻松更新所需的属性值:

After fixing that, you can easily update the needed property value:

jq '.Mappings.RegionMap["us-east-1"].AMI = "abcd"' oldfile > newfile

newfile内容:

newfile contents:

{ "Name": "SA", "Password": "yyyyy", "Mappings": { "RegionMap": { "us-east-1": { "AMI": "abcd" }, "us-east-2": { "AMI": "" }, "us-west-1": { "AMI": "" }, "us-west-2": { "AMI": "" }, "ca-central-1": { "AMI": "" }, "eu-central-1": { "AMI": "" }, "eu-west-1": { "AMI": "" }, "eu-west-2": { "AMI": "" }, "ap-south-1": { "AMI": "" }, "ap-southeast-1": { "AMI": "" }, "ap-southeast-2": { "AMI": "" }, "ap-northeast-1": { "AMI": "" }, "ap-northeast-2": { "AMI": "" }, "sa-east-1": { "AMI": "" } } } }

更多推荐

使用重定向运算符通过jq更新文件内容

本文发布于:2023-11-28 13:37:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   重定向   文件   内容   jq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!