Home > Software design >  Bash script - unable to replace string with double quotes and curly braces
Bash script - unable to replace string with double quotes and curly braces

Time:05-29

I'm struggling for some time and I would need some help with the following operation.

I have a JSON file and I would like to replace a string with something a bit more complex.

This is a snippet of my json file:

{ "AWS679f53fac002430cb0da5b7982bd22872D164C4C": {
   "Type": "AWS::Lambda::Function",
   "Properties": {
    "Code": {
     "S3Bucket": "hnb659fds-assets-xxccddff",
     "S3Key": "68b4ffa1c39cb3733535725f85311791c09eab53b7ab8efa5152e68f8abdb005.zip"
    },
    "Role": {
     "Fn::GetAtt": [
      "AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2",
      "Arn"
     ]
    },
    "Handler": "index.handler",
    "Runtime": "nodejs12.x",
    "Timeout": 120
   },
   "DependsOn": [
    "AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2"
   ],
   "Metadata": {
    "aws:cdk:path": "CODE/AWS679f53fac002430cb0da5b7982bd2287/Resource",
    "aws:asset:path": "asset.68b4ffa1c39cb3733535725f85311791c09eab53b7ab8efa5152e68f8abdb005",
    "aws:asset:is-bundled": false,
    "aws:asset:property": "Code"
   }
  }
}

What I need is to replace this part

 "S3Bucket": "hnb659fds-assets-xxccddff",

and have the following result

"S3Bucket": {"Fn::Sub": "AAA-${AWS::Region}" },

I don't know the AWS679f53fac002430cb0da5b7982bd22872D164C4C. It is generated randomly and the string to replace is present several times in my json file.

The initial values to be replaced is stored in a variable along with the new value to be used in the replaced version as following:

cdk_bucket_name=hnb659fds-assets-xxccddff
OUTPUT_BUCKET=AAA

I need these variables because this is part of a bigger script

So I tried some sed but does not work

new_bucket_name="{"Fn::Sub\": \"$OUTPUT_BUCKET-${AWS::Region}\" }"

sed -i "s#$cdk_bucket_name#$new_bucket_name#g" my.template.json

One issue that I have is that ${AWS::Region} gets interpreted so is empty. And second, I cannot manage the quotes in order to have my desired result.

CodePudding user response:

Using sed

$ output_bucket=AAA
$ new_bucket_name="{\"Fn::Sub\": \"$output_bucket-\${AWS::Region}\" }"
$ cdk_bucket_name=hnb659fds-assets-xxccddff
$ sed s"/\"$cdk_bucket_name\"/$new_bucket_name/" input_file
{ "AWS679f53fac002430cb0da5b7982bd22872D164C4C": {
   "Type": "AWS::Lambda::Function",
   "Properties": {
    "Code": {
     "S3Bucket": {"Fn::Sub": "AAA-${AWS::Region}" },
     "S3Key": "68b4ffa1c39cb3733535725f85311791c09eab53b7ab8efa5152e68f8abdb005.zip"
    },
    "Role": {
     "Fn::GetAtt": [
      "AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2",
      "Arn"
     ]
    },
    "Handler": "index.handler",
    "Runtime": "nodejs12.x",
    "Timeout": 120
   },
   "DependsOn": [
    "AWS679f53fac002430cb0da5b7982bd2287ServiceRoleC1EA0FF2"
   ],
   "Metadata": {
    "aws:cdk:path": "CODE/AWS679f53fac002430cb0da5b7982bd2287/Resource",
    "aws:asset:path": "asset.68b4ffa1c39cb3733535725f85311791c09eab53b7ab8efa5152e68f8abdb005",
    "aws:asset:is-bundled": false,
    "aws:asset:property": "Code"
   }
  }
}

CodePudding user response:

Using a proper JSON parser shell tool like jq:

jq '
(
  .[].Properties.Code.S3Bucket |
    select(. == "hnb659fds-assets-xxccddff")
) = $newS3Bucket
' input_file.json \
  --argjson newS3Bucket '{"Fn::Sub":"AAA-${AWS::Region}"}'
  • Related