mskew
mskew

Reputation: 105

Sed delimiter options

In my code, I need to replace variable assignments with addresses:

sed -i "s/^variable = .*$/variable = http://myaddress/"

Obviously, this does not work because the forward slashes in the address are recognized in the sed command.

I want to keep the $ at the end of the first expression for replacing anything to the end of the line. I also do not want to escape the dollar sign as such, \$ because it will search for a dollar sign.

Also, I don't want to just escape the forward slashes in the address as there are also variables in some places for the addresses.

I've tried using # instead of / but have to include what I don't want to - the \$.

Are there any alternate delimiters I can use that fit my situation?

Upvotes: 0

Views: 1503

Answers (3)

Ed Morton
Ed Morton

Reputation: 203995

sed -i 's#^variable = .*#variable = http://myaddress#'

Upvotes: 0

Kent
Kent

Reputation: 195179

sed -i "s@^variable = .*$@variable = http://myaddress@" file

should work for you.

Note that the $ in the first expression is not literature "dollar", but a regex expression, means, the end of the line.

Upvotes: 0

t-8ch
t-8ch

Reputation: 2713

The $ is interpreted by your shell. Wrap the whole argument to sed with ' to prevent this.

sed -i 's#^variable = .*$#variable = http://myaddress#'

Upvotes: 2

Related Questions