Json文件如下:
{
"ImageId": "ami-074acc26872f26463",
"KeyName": "Accesskeypair",
"SecurityGroupIds": ["sg-064caf9c470e4e8e6"],
"InstanceType": ""
"Placement": {
"AvailabilityZone": "us-east-1a"
}
}
读取类型#user的输入
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”
答案 0 :(得分:2)
您可以使用轻量级JSON流程或称为 jq
加载文件后,可以使用jq'.InstanceType =“这是实例值”
答案 1 :(得分:0)
使用-e
选项不会修改文件,而是会在终端中打印输出。
-i
选项将修改文件。
使用.*
正则表达式将更新文件和具有任何值的特定键
并使用\"\"
只会查找空字符串并更新值。
尝试:
sed -i "s/\"InstanceType\":.*,$/\"InstanceType\": \"${instance_type}\",/g" awsspotinstancecreation.json
OR
sed -i "s/\"InstanceType\": \"\",$/\"InstanceType\": \"${instance_type}\",/g" awsspotinstancecreation.json