sed replace

Hi,

Below is the code. The issue is exact value of echo $URL is not replaced in sed in side the function. Any hints..

#!/bin/bash

envurls()
{

URL=`cat temp.txt | sed -e 's%\/%\\\/%g' | grep client.url |cut -d "=" -f 2`

echo $URL

sed 's#CLIENTURL#'$URL'#' temp1.properties>temp && mv temp temp1.properties

cat temp1.properties

}

envurls

temp1.properties:

ppend_desc='\<br\>\<br\>\
\n\<b\>alc131AIXqa199\<\/b\>\
\n\<br\>\<br\>\<a href=\"CLIENTURL br\>\&lt'

temp.txt:

client.url=http://google.com/images
wiki.url=

Current Output:

ppend_desc='\<br\>\<br\>\
\n\<b\>alc131AIXqa199\<\/b\>\
\n\<br\>\<br\>\<a href=\"http://google.com/images br\>\&lt'

Expected Output:

ppend_desc='\<br\>\<br\>\
\n\<b\>alc131AIXqa199\<\/b\>\
\n\<br\>\<br\>\<a href=\"http:\/\/google.com\/images br\>\&lt'

Best,
Vinodh Kumar

I see what you mean now, it seems to remove the backslashes with sed thinking they are escape characters.. maybe use double backslashes?

No. It doesn't work out. It assumes $URL as string value.

The output:

ppend_desc='\<br\>\<br\>\
\n\<b\>alc131AIXqa199\<\/b\>\
\n\<br\>\<br\>\<a href=\""$URL" br\>\&lt'

Right, try calling sed from a file instead, perhaps that will make it easier to keep the backslashes in the output. I tried escaping the backslashes with backslashes but they all get removed.

When you change the delimiter from / to % or #, / ceases to be a special character and is now an ordinary character. According to the standard, quoting an ordinary character leads to undefined behavior. So, your

sed -e 's%\/%\\\/%g' 

should be

sed -e 's%/%\\/%g'

in order to behave consistently.

Now, to protect the backslash from being consumed by the second sed, you need to escape it as well, so that the first sed becomes:

sed -e 's%/%\\\\/%g'

Regards,
Alister

You can also simplify the URL variable assignment like so:

URL=`sed -ne '/client.url/{s/^.*=//,s%/%\\\\/%g,p}' temp.txt`

Explanation:
-n don't print lines by default
/client.url/ Act only on lines with this address or matching this pattern. Could also be 1 or 1,1 or 1,/url=/ etc.
{} a multiline statement
, separate multiple statements with commas

Incorrect. That will yield twice as many backslashes in the final result than are required. Instead of one backslash before a forward slash, the final output will contain two backslashes before each forward slash.

With regard to "sed needs triple escapes", I'm not sure what you mean. One backslash quotes a special character (whether in a regular expression or the substitute command's replacement text). That's all there is to it.

Regards,
Alister

P.S. Welcome to the forum :slight_smile:

I think the issue is with secone sed. Any hints....

sed -e 's%/%\\\\/%g'

This doesn't work out!!!!

Try it with the triple quoted backslash to see if it works.
\\\\
instead of single quoted
\\
It would help if you showed the out put of what doesn't work. Including the output of the echo of $URL.