0

I have a script that runs through all the files in my local directory and finds key words in my terraform files. The issue I'm facing is that I'm unable to replace defined variables in the files. When I reference value to be replaced, it captures $variable as the reference instead. For example:

Repository_Name="test"

grep -rl '#{RepositoryName}#' --include *.tf |
  xargs gsed -i 's/#{RepositoryName}#/${Repository_Name}/g'

I've also tried removing the {} outside the variable like the example below and still no luck:

Repository_Name="test"

grep -rl '#{RepositoryName}#' --include *.tf |
  xargs gsed -i 's/#{RepositoryName}#/$Repository_Name/g'

The result I usually get is:

RepositoryName = "$Repository_Name"

or

RepositoryName = "${Repository_Name}"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441

3 Answers3

0

You need " for the variables to be evaluated

Repository_Name="test"

grep -rl '#{RepositoryName}#' --include *.tf | xargs gsed -i "s/#{RepositoryName}#/${Repository_Name}/g"

You may also need extra { if you want them in the file.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Actually sed is enough:

sed -i '/#{RepositoryName}#/'"s||$Repository_Name|g" *.tf
Ivan
  • 6,188
  • 1
  • 16
  • 23
0

The solution to this issue

grep -rl '#{RepositoryName}#' --include *.tf |
  xargs gsed -i "s@#{RepositoryName}#@${Repository_Name}@g"

There were char complications with sed.

for reference

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441