Global replace with perl in bash

I am newbie to perl,
I am trying to use the below syntax to replace globally a string with a variable.

$ bash -version
GNU bash, version 2.05b.0(1)-release (powerpc-ibm-aix5.1)
Copyright (C) 2002 Free Software Foundation, Inc.

$ perl -version
This is perl, v5.8.8 built for aix-thread-multi
# !/bin/bash
set -x
REF_URL="http://vmdis01.corp.testcompany.web/ABC_1.0_Version/index.htm"
cp test_url_main.txt test_url.txt
perl -pi -e "s/XXREF_URLXX/\Q${REF_URL}\E/g;" test_url.txt
cat test_url.txt


I am getting the following error

++ REF_URL=http://vmdis01.corp.testcompany.web/ABC_1.0_Version/index.htm
++ cp test_url_main.txt test_url.txt
++ perl -pi -e 's/XXREF_URLXX/\Qhttp://vmdis01.corp.testcompany.web/ABC_1.0_Version/index.htm\E/g;' test_url.txt
Bareword found where operator expected at -e line 1, near "0_Version"
        (Missing operator before Version?)
Backslash found where operator expected at -e line 1, near "htm\"
syntax error at -e line 1, near "0_Version"
Execution of -e aborted due to compilation errors.
++ cat test_url.txt
XXREF_URLXX


Any help is appreciated.

This has to do with your choice of delimeter (/), since the string would expand to:

perl -pi -e "s/XXREF_URLXX/\Qhttp://vmdis01.corp.testcompany.web/ABC_1.0_Version/index.htm\E/g;" test_url.txt

...not a valid substitution command.

Try using a different one.

perl -pi -e "s|XXREF_URLXX|\Q${REF_URL}\E|g;" test_url.txt
1 Like

That worked like charm, and thanks for the speedy reply !!!