escaping backslashes to evaluate paths

Hi there,

i am struggling with this one, basically i want to replace an existing path string in a file with a new one, but the new one contains slashes which causes problems with "sed", i thought i could change the replacement string into a variable but of course when the variable is evaluated it contains slashes which confuses sed , so i thought i could set the original variable with escaped slashes but it doesn't like this.
the example is:

I want to change a line in a file:
CLASSPATH="/opt/app/example.jar"
to:
CLASSPATH="/opt/app/default.jar"

i was going to use "sed" or perl pie to do this, e.g
sed -e "s/^CLASSPATH.*$/CLASSPATH=\"${CLASSES}\"/g" set.sh >set.out

CLASSES is an environment variable that is populated from a properties file so the sed line above just replaces the whole line that begins with CLASSPATH with the new line based on the variable. but i need to escape the backslashes when the variable is evaluated so that they get pushed into the "sed" command.

any good ideas?

cheers

Steve

Something like this:

 
echo "CLASSPATH=/opt/app/example.jar" | sed 's!/opt/app/example.jar!/opt/app/default.jar!'

Try this...

kamaraj@kamaraj-laptop:~/Desktop/testing$ export abc="\"/opt/app/default.jar\""

kamaraj@kamaraj-laptop:~/Desktop/testing$ echo $abc
"/opt/app/default.jar"

kamaraj@kamaraj-laptop:~/Desktop/testing$ cat path_file 
CLASSPATH="/opt/app/example.jar"

kamaraj@kamaraj-laptop:~/Desktop/testing$ sed -ie "s#^CLASSPATH=.*#CLASSPAHT=$abc#g" path_file 

kamaraj@kamaraj-laptop:~/Desktop/testing$ cat path_file
CLASSPAHT="/opt/app/default.jar"


hi there,

the problem i have is that i can't always guarantee that the string in the original file is the same which is why i chose to replace the whole line if the line starts with CLASSPATH.
The replacement PATH might also be different according to what has been set in a properties file.

so the original file could have something like:
CLASSPATH=/opt/app/default.jar
or
CLASSPATH=/tmp/process.jar
and so on and so forth..

And the replacement string might be something like:
/opt/app/process.jar
or
/opt/app/temp.jar:/var/tmp.boogie.jar
or
/tmp/login.jar:/tmp/process.jar:/root.jar
etc.etc.etc.etc

so the first step i have taken is to assign the replacement string to a variable, then i want the variable to be evaluated into the "sed" command but at the same time replacing the "/"s with "\/"s so that sed won't barf.

---------- Post updated at 11:37 AM ---------- Previous update was at 11:31 AM ----------

@itkamaraj

thats spot on, worked a treat using "#"s in the sed command rather than "/"s.

nice one, thankyou.

Steve

you are welcome