0

I'm trying to use sed to replace a specific line within a configuration file:

The pattern for the line I want to replace is:

ALLOWED_HOSTS.*

The text I want to insert is:

'$PublicIP' (Including the single ticks)

But when I run the command:

sed 's/ALLOWED_HOSTS.*/ALLOWED_HOSTS = ['$PublicIP']/g' /root/project/django/mysite/mysite/settings.py

The line is changed to:

ALLOWED_HOSTS = [1.1.1.1]

instead of:

ALLOWED_HOSTS = ['1.1.1.1']

How shall I edit the command to include the single ticks as well?

Itai Ganot
  • 5,873
  • 18
  • 56
  • 99

1 Answers1

1

You could try to escape the single ticks , or better you can reassign the variable including the simple ticks:

PublicIP="'$PublicIP'".

By the way even this sed without redifining var, works ok in my case:

$ a="3.3.3.3"
$ echo "ALLOWED_HOSTS = [2.2.2.2]" |sed 's/2.2.2.2/'"'$a'"'/g'
ALLOWED_HOSTS = ['3.3.3.3']

Even this works ok:

$ echo "ALLOWED_HOSTS = [2.2.2.2]" |sed "s/2.2.2.2/'$a'/g"
ALLOWED_HOSTS = ['3.3.3.3']
George Vasiliou
  • 6,130
  • 2
  • 20
  • 27