user1259318
user1259318

Reputation: 21

replacing string in a shell script containing specialcharacters using sed

I am facing problem with sed command to replace a text. The sed command works perfectly fine when the replacement text is a plane text without any special charachter. But my replacement text includes the unix file system path and hence it is not working. I am getting an error as below, some one please help me in this :

$ ./install2
sed: -e expression #1, char 22: unknown option to `s'
sed: -e expression #1, char 16: unknown option to `s'
$ cat install2
#!/bin/bash
CONFIG=./config
TMPFILE=./tmpFile

while read line
do

        var=$(echo $line | cut -d'=' -f1)
        val=$(echo $line | cut -d'=' -f2)
        sed -i "s/$var/${val}/i" $TMPFILE

done < "$CONFIG"

$ cat config
INSTALLATION_ROOT=/opt/install/user-home/products
DOWNLOAD_HIBERNATE=false
CONFIG_ROOT=/opt/configs/cfgMgmt/products

$ cat tmpFile
<entry key="installationRoot">INSTALLATION_ROOT</entry>
<entry key="configDirectoryRoot">CONFIG_ROOT</entry>

Thanks, Sunny

Upvotes: 1

Views: 1004

Answers (2)

kev
kev

Reputation: 161614

The / characters may be uniformly replaced by any other single character within any given s command.
The / character (or whatever other character is used in its stead) can appear in the regexp or replacement only if it is preceded by a \ character.

sed -i "s@\$var@${val}@i" $TMPFILE

Upvotes: 1

ks1322
ks1322

Reputation: 35708

Try this sed -i "s|$var|${val}|i" $TMPFILE. Use | instead of / to allow path replacement.

Upvotes: 1

Related Questions