1

I'm trying to replace a string in a file using a script in linux.

After reading this post : Find and Replace Inside a Text File from a Bash Command

I used the following line

perl -pi -e "s/myparam/$MY_VAR/g" /res/raw/configuration.xml

But the problem is that MY_VARcontains a url (something like https://stackoverflow.com/questions/ask), which contains / and it doesn't replaces the string at all.

I assume i don't need to write a script that does find and replace to all the / and there is a simple way to solve it.

Thanks

Community
  • 1
  • 1
OopsUser
  • 4,642
  • 7
  • 46
  • 71

2 Answers2

1

You can use this sed with alternate delimiter:

sed -i.bak "s~myparam~$MY_VAR~g" /res/raw/configuration.xml

Perl also allows same so use:

perl -pi -e "s~myparam~$MY_VAR~g" /res/raw/configuration.xml
anubhava
  • 761,203
  • 64
  • 569
  • 643
1
perl -pi -e 's/myparam/$ENV{MY_VAR}/g' /res/raw/configuration.xml

or put $MY_VAR on command line instead of interpolating it into perl one liner,

perl -pi -e 'BEGIN{$s = shift} s/myparam/$s/g' $MY_VAR /res/raw/configuration.xml
mpapec
  • 50,217
  • 8
  • 67
  • 127