Passing in regular expression as a parameter to sed

Hi All,

Im using Bash.
I have a pipe delimited config file which contains 3 columns,
1st column = location of a file to be chnaged
2 column = expression to find within file
3rd column = string to replace.

A script will loop through the contetnts of this file and apply the changes using sed.

The issue I have is that i cannot seem to get the metacharacters to work when passed in from the config file into the sed expression.

Example:
The below exmaple works fine for the string in the file to be changed:
string to be changed:
<rmi-port>13093</rmi-port>
line in config file:
samplefile.xml|<rmi-port>[0-9][0-9]|<rmi-port>18
via sed in the script, the resultant change becomes
<rmi-port>18093</rmi-port>

The problem occurs when I use simple metacharacters, no changes take place:
example using ^ for start of the : samplefile.xml|^<rmi-port>[0-9][0-9]|<rmi-port>18

The above will not work. It has to be an issue with the way the regular expression is being interpreted by the sed command within the script.

The sed command issued is:
sed -e "s/${search_string}/${replace_string}/g" $dir_file_backup > $dir_file

Any help is greatly aprreciated!

Kind Regards
Satnam

Works fine for me (on AIX box) :

$ cat sat.ksh
while IFS='|' read file search_string replace_string
do
    sed -e "s/${search_string}/${replace_string}/g"  ${file} > ${file}.tmp && \
    mv ${file}.tmp ${file}
done <sat.dat
$ cat sat.xml
<rmi-port>13093</rmi-port>
$ cat sat.dat
sat.xml|^<rmi-port>[0-9][0-9]|<rmi-port>18
$ ./sat.ksh
$ cat sat.xml
<rmi-port>18093</rmi-port>
$

Jean-Pierre.

much obliged for your feedback! :slight_smile: