Issue
tr file and trying to change data in it and getting this substitution error.
my file data is
variable "aws_spoke_gateways" {
default = {
spoke1 = {
name = "AWS-UE2-Spoke1-GW"
},
spoke2 = {
name = "AWS-UE2-Spoke1-GW"
}
}
}
And My desired output is
variable "aws_spoke_gateways" {
default = {
spoke1 = {
name = "myname"
},
spoke2 = {
name = "AWS-UE2-Spoke1-GW"
}
}
}
EDIT 1
I want to change the values of name , size , and vpc and both spoke1
and spoke2
variable "aws_spoke_gateways" {
default = {
spoke1 = {
name = "AZ-EU-Spoke1-GW"
size = "Standard_B1ms"
active_mesh = true
single_az_ha = true
vpc = "azure_spoke2_vnet"
},
spoke2 = {
name = "AZ-EU-Spoke2-GW"
size = "Standard_B1ms"
active_mesh = true
single_az_ha = true
vpc = "azure_spoke2_vnet"
}
}
}
Solution
Use ranges to make sure you get the right one. Here's a quick example you can refine -
$: sed '/spoke1/,/\},/ { s/AWS-UE2-Spoke1-GW/myname/; }' file
variable "aws_spoke_gateways" {
default = {
spoke1 = {
name = "myname"
},
spoke2 = {
name = "AWS-UE2-Spoke1-GW"
}
}
}
If you need more granular control, you can nest the ranges.
$: sed '/variable "aws_spoke_gateways" \{/,/^\},/ {
/spoke1/,/\},/ { s/AWS-UE2-Spoke1-GW/new1/; }
/spoke2/,/\},/ { s/AWS-UE2-Spoke1-GW/new2/; }
}' file
variable "aws_spoke_gateways" {
default = {
spoke1 = {
name = "new1"
},
spoke2 = {
name = "new2"
}
}
}
Note that I added an anchor on the ending brace of the outer wrapper: ^\},
The manual is your friend. Read it all the way through once, you'll save enough hours to make up for it.
Answered By - Paul Hodges Answer Checked By - Candace Johnson (WPSolving Volunteer)