0

I have something like this in file and I want to change few values

My file data

variable "my_vnets" {
  default = {
    transit_vnet = {
      name       = "dummy"
      cidr       = "11.1.1.1.1."
      is_transit = false
    }
    spoke1_vnet = {
      name       = "dummy2"
      cidr       = "1.1.1.1.1"
      is_transit = false
    }
  }
}

expected output

variable "my_vnets" {
  default = {
    transit_vnet = {
      name       = "NewName"
      cidr       = "11.1.1.1.1."
      is_transit = true
    }
    spoke1_vnet = {
      name       = "NewName2"
      cidr       = "2.2.2.2"
      is_transit = false
    }
  }
}

I am trying something like this but no luck

sed -i "s/\"my_vnets\" { default = \"""\" }/\"my_vnets\" { default = \""NewName"\" }/g"
Cyrus
  • 84,225
  • 14
  • 89
  • 153

1 Answers1

1

In general, usage of structure-unaware tools to edit structured files should be avoided. However, if there aren't any structured tools for parsing and generating this format, consider something like:

sed -r -e '/^[[:space:]]+name[[:space:]]+=/ { s/dummy/NewName/; }' \
       -e '/transit_vnet = [{]/,/[}]/       { s/is_transit = false/is_transit = true/; }'

...replacing dummy with NewName only on lines that start with name = ; and is_transit = false with is_transit = true only after a match for transit_vnet = { and before a match for }.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • thanks for reply sir , there is one constraint . for example I want to change `is_transit` value then whole file consisting of 'is_transit' variable will be changed. thats why I need to be specific to a variable . – Shahroz Pervaz Nov 30 '20 at 18:55
  • I don't think your question currently specifies that constraint -- your sample data edits _both_ names. – Charles Duffy Nov 30 '20 at 18:56
  • ...that said, does applying that edit between the line `transit_vnet {` and the next-following line with only a `}` work? – Charles Duffy Nov 30 '20 at 18:57
  • I have ` default = { transit_vnet = { name = "NewName" cidr = "11.1.1.1.1." is_transit = true } spoke1_vnet = { name = "NewName2" cidr = "2.2.2.2" is_transit = false }` **2** `is_transit` variables . So I just want to change one of them – Shahroz Pervaz Nov 30 '20 at 18:57
  • 1
    See the edit, changing `is_transit` only inside the `transit_vnet` block. – Charles Duffy Nov 30 '20 at 19:00
  • got it . is it possible to to change name of just `transit_vnet` block , As I am not sure if it in future name of both can be same as `dummy` – Shahroz Pervaz Nov 30 '20 at 19:08
  • Of course, just use the same technique shown above for `is_transit` for `name` instead. – Charles Duffy Nov 30 '20 at 19:14
  • `sed -r -e '/transit_vnet = [{]/,/[}]/ { s/name = dummy/name = myname/; }g' /variables.tf` doing this but not changing Name :( – Shahroz Pervaz Nov 30 '20 at 19:45
  • You're matching only exact `name = dummy`, there, but not with the extra spaces. – Charles Duffy Nov 30 '20 at 19:50
  • Still I am unable to figure out sir , can you please update your answer. – Shahroz Pervaz Nov 30 '20 at 21:24
  • The answer answers the question you asked. To edit it would make it answer a different question, which you did not ask. Thus, I do not intend to edit it further. – Charles Duffy Nov 30 '20 at 23:36
  • sir I am stuck on a small issue . need help in case of ***name*** what I will use instead of this `[{]/,/[}]/ ` – Shahroz Pervaz Dec 01 '20 at 21:06