Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed gives error with unterminated substitute in regular expression

Tags:

shell

sed

awk

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"
}
like image 768
Sanjay Shitole Avatar asked Mar 28 '18 17:03

Sanjay Shitole


1 Answers

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"
like image 118
that other guy Avatar answered Nov 15 '22 09:11

that other guy