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

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

$ENV{MY_VAR} would be the natural way to use a shell variable.
@TLP tnx for comment.
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

2 Comments

You can also use an alternate delimiter in Perl.
Yes right, I was just verifying it first before adding that to answer. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.