Created
March 22, 2024 17:27
-
-
Save skarllot/8ae1d5484417ad6cd954ee2143561b31 to your computer and use it in GitHub Desktop.
Terraform multiple replaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ref: https://stackoverflow.com/a/67392622/23266117 | |
locals { | |
input = file("./sample.txt") | |
map = { | |
food = "Sushi" | |
name = "Data_sniffer" | |
} | |
out = join("\n", [ | |
for line in split("\n", local.input) : | |
format( | |
replace(line, "/{(${join("|", keys(local.map))})}/", "%s"), | |
[ | |
for value in flatten(regexall("{(${join("|", keys(local.map))})}", line)) : | |
lookup(local.map, value) | |
]... | |
) | |
]) | |
} | |
output "test_out" { | |
value = local.out | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello {name}, my favorite food is {food} I'd | |
like to eat {food} and sleep. | |
Here's a {non_existent} variable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment