Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash script – unable to replace string with double quotes and curly braces

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{ "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.

>Solution :

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"
   }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading