I trying to substitute string "x.x.x.x" in a file and output the results to another file. It is giving me error unterminated substitute in regular expression
here is the code
sed 's/x.x.x.x/'$bigip_management_ip'/g' temp.tt >> variables.tf
sed: 1: "s/x.x.x.x/": unterminated substitute in regular expression
echo $bigip_management_ip
54.83.174.153
shitole$ cat temp.tt
variable "bigip_management_ip" {
default = "x.x.x.x"
}
Here's a MCVE for your problem:
bigip_management_ip=" 54.83.174.153"
sed 's/x.x.x.x/'$bigip_management_ip'/g'
When executed on macOS, you get:
sed: 1: "s/x.x.x.x/": unterminated substitute in regular expression
The problem is the leading space in the variable causing word splitting due to a lack of quoting. ShellCheck warns about this:
In /Users/myuser/myscript line 2:
sed 's/x.x.x.x/'$bigip_management_ip'/g'
^-- SC2086: Double quote to prevent globbing and word splitting.
You should always quote your variables unless you're sure you can't:
sed "s/x.x.x.x/$bigip_management_ip/g"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With