gpt4 book ai didi

json - 如何使用bash脚本将json值添加到json文件

转载 作者:行者123 更新时间:2023-12-03 08:54:35 24 4
gpt4 key购买 nike

Json文件如下:

{

"ImageId": "ami-074acc26872f26463",
"KeyName": "Accesskeypair",
"SecurityGroupIds": ["sg-064caf9c470e4e8e6"],
"InstanceType": ""
"Placement": {
"AvailabilityZone": "us-east-1a"
}
}

从用户读取类型#input

现在我想使用 bash 脚本在文件中的“InstanceType”:“$type”中添加 json 值,我尝试过 sed、cat 和 echo

echo "Hello"
echo "lets create an instance"
echo "please enter the type of instance you need"
read instance_type;
echo $type
sed -a '|\InstanceType": "|$instance_type|a' awsspotinstancecreation.json
sed -e '/\"InstanceType": "|$instance_type|/a' awsspotinstancecreation.json
sed -i "/\InstanceType": "/ s/^\(.*\)\('\)/\1, $instance_type/" awsspotinstancecreation.json
sed -e 's/^"InstanceType": "",.*$/"InstanceType": "$instance_type",/g' awsspotinstancecreation.json
sed -i 's/^InstanceType .*$/"InstanceType": "$instance_type",/' awsspotinstancecreation.json
sed -i 's:^"InstanceType": "",.*$:"InstanceType": "$instance_type",:a' awsspotinstancecreation.json

但是当我这样做时,它出现的是“InstanceType”:“$type”而不是“InstanceType”:“t2.small”

最佳答案

您可以使用名为 jq 的轻量级 JSON 处理器

加载文件后,您可以使用 jq '.InstanceType = "This is instance value"

可以引用Add a new element to an existing JSON array using jq

关于json - 如何使用bash脚本将json值添加到json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56523219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com