Score:0

Need help in replacing values in json using sed

mx flag

Hello Team i need an assistance in replacing the current ecs task defintion revision in json file. I'm able to replace the values for awsNetworkSubnet in variables.json successfully. But struggling to create apprpriate sed command for revision no (which is 7 in sample variables.json below)

Bash Script:

#!/bin/bash
Input_file=$1
ENV=$2
Commit_id=$3

subnet_id=$(aws ec2 describe-subnets --filter "Name=tag:use,Values=data" --query "Subnets[*].SubnetId" --output json | jq -r '.[0]')
echo "Subnet ID: ${subnet_id}"

airflow_ecs_task_definition_revision=$( aws ecs describe-task-definition --task-definition airflow_ecs-small --output json | jq '.taskDefinition.revision')
echo "ECS Task Definition Revision: ${airflow_ecs_task_definition_revision}"

sed -i $1 \
    -e "/\"awsNetworkSubnet\":/ s/\"awsNetworkSubnet\":[^,]*/\"awsNetworkSubnet\":\"${subnet_id}\"/"

Sample Json file (variables,json)

        "awsCluster": "airflow_ecs-dev",
        "awsContainerName": "airflow_ecs",
        "awsNetworkSubnet": "subnet-1234",
        "awsRegionName": "us-east-1",
        "awsSecurityGroup": "sg-1234",
        "awsTaskDefinition": "airflow_ecs-small:7"
    },

AWS CLi Used:

./subnet-cicd.sh variables.json test cmt1234
cn flag
The standard advice is: use a JSON parser to parse JSON data. [`jq`](https://stedolan.github.io/jq/) for example
hr flag
Yeah I'm confused - you seem to be aware of jq - perhaps what you are missing is the *update assignment* operator `|=`? See [Assignment](https://stedolan.github.io/jq/manual/#Assignment)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.