【发布时间】:2019-10-24 16:00:25
【问题描述】:
Json文件如下:
{
"ImageId": "ami-074acc26872f26463",
"KeyName": "Accesskeypair",
"SecurityGroupIds": ["sg-064caf9c470e4e8e6"],
"InstanceType": ""
"Placement": {
"AvailabilityZone": "us-east-1a"
}
}
从用户读取类型#input
现在我想使用 bash 脚本在“InstanceType”中添加 json 值:“$type”我尝试过 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"
【问题讨论】:
-
请返回并编辑您的original question。
-
您的文件是无效的 json。您的 InstanceType 值后缺少逗号。就像我在另一个线程中所说,不要使用
sed,使用jq