Mike
Mike

Reputation: 7914

replace using sed

I want to replace IP dynamically but somehwo sed is placing word $IP instead of actual value.

 IP=10.50.33.44
 PORT=5774
 sed -i~ 's/https:\/\/10.11.12.13:8443/https:\/\/$IP:$PORT/g' abc.txt

Can you help me out in getting the correct value?

Upvotes: 1

Views: 358

Answers (2)

potong
potong

Reputation: 58473

Variation on a theme: I always use single quotes to surround sed/awk/perl... commands as the shell can sometimes trip you up when using double quotes. I find it best to double quote the variables:

sed -i~ 's/https:\/\/10.11.12.13:8443/https:\/\/'"$IP"':'"$PORT"'/g' abc.txt

As a "belt and braces" and as I usually compose my commands interactively at the command line in bash, the key-binding M-C-e (that's Alt-Control-e on most keyboards) will interpolate the command before it's sent. Letting you visually see what the command is really getting.

Upvotes: 0

Prince John Wesley
Prince John Wesley

Reputation: 63698

Use double quotes" for variable expansion:

 sed -i~ "s/https:\/\/10.11.12.13:8443/https:\/\/$IP:$PORT/g" abc.txt

and as @Joachim said, use different delimiter. For example,

 sed -i~ "s;https://10.11.12.13:8443;https://$IP:$PORT;g" abc.txt

Upvotes: 3

Related Questions