escaping backslashes to evaluate paths


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting escaping backslashes to evaluate paths
# 1  
Old 05-10-2010
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
# 2  
Old 05-10-2010
Something like this:

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

# 3  
Old 05-10-2010
Try this...

Code:
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"


Last edited by itkamaraj; 05-10-2010 at 08:35 AM..
# 4  
Old 05-10-2010
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
# 5  
Old 05-10-2010
you are welcome
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to add backslashes to end of certain lines of text

I'd like to write up notes in a relatively readable format and then use a shell script to add LaTeX formatting. Specifically, I'm trying to figure out how to add the LaTeX newline character (\\) to the end of lines without \begin{} or \end{} statements example notes file: \begin{enumerate} --... (2 Replies)
Discussion started by: icskittles
2 Replies

2. UNIX for Dummies Questions & Answers

How does ||: evaluate?

In BASH, how does ||: get interpreted. I know || is logical or. And I believe : evaluates to true. Can someone give a thorough explanation for this usage? Example for i in $IGGY do && skipdb=1 || : (6 Replies)
Discussion started by: glev2005
6 Replies

3. Shell Programming and Scripting

escaping metacharacters in paths for a shell command

I have a file which contains a list of paths separated by a new line character. e.g /some/path/to/a/file.png /some/path to/another/file.jpeg /some path/to yet/another/file Notice that these paths may contain metacharacters, the spaces for example are also not escaped. If I wanted... (5 Replies)
Discussion started by: cue
5 Replies

4. UNIX for Dummies Questions & Answers

printing backslashes

printf "\\" prints a backslash.how we can print multiple backslashes such as "\\\\\\\\\\\\\\\\\\\\". Thanks (4 Replies)
Discussion started by: pandeesh
4 Replies

5. Shell Programming and Scripting

Replacing 3 backslashes with 2 in a string

Hi, I have a requirement where i need to replace 3 backslashes with 2 in the below mentioned string, but i am facing problem with backslashes. However i tried the option below but doesnt work. My input remains a constant as i receive it from my upstream. Input String= "-rfile... (3 Replies)
Discussion started by: kk_madrid
3 Replies

6. Shell Programming and Scripting

Evaluate the value of a variable?

I have variables: FOO="Text" BAR="FOO" I'd like to be able to evaluate the variable named as the value of $BAR. echo $FOO Text echo $BAR FOO This is what I'd like to do: echo ${$BAR} (this won't work) Text (3 Replies)
Discussion started by: Ilja
3 Replies

7. UNIX for Dummies Questions & Answers

Backslashes in Filenames

Using a small script, I automatically generated some text logs. The files ended being undownloadable, unopenable and undeletable. Upon further investigation, the files ended up looking like this: log\r log2\r log3\r I've tried a few different things, including double slashing before the... (6 Replies)
Discussion started by: shepherdsflock
6 Replies

8. Shell Programming and Scripting

Trouble with Backslashes

Hi folks, there are windows device names in the sixth column of a comma separated file. A example device name is: \\.\Tape0 I don't get the all string in to a variable, because of the preceding backslash. The first backslash is just cut off and my attempts to manipulate the string afterward... (0 Replies)
Discussion started by: wibo1
0 Replies

9. Shell Programming and Scripting

How to echo 4 backslashes more easy ?

How to echo 4 backslashes more easy ? I can use 16 to echo 4 backslashes. # echo \\ \ # echo \\\\ \ # echo \\\\\\ \\ # echo \\\\\\\\ \\ # echo \\\\\\\\\\ \\\ # echo \\\\\\\\\\\\ \\\ # echo \\\\\\\\\\\\\\ \\\\ (4 Replies)
Discussion started by: sun-guy
4 Replies

10. Shell Programming and Scripting

how to get variable to re-evaluate itself?

Probably a simple one. Basically I am retrieving a number from a file - setting a variable against it and then incrementing this by 1 and using this as an entry number in a log file for messages. I need the variable to re-evalute itself each time I call it so I get the latest number in the file -... (1 Reply)
Discussion started by: frustrated1
1 Replies
Login or Register to Ask a Question