sed replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace
# 1  
Old 03-08-2011
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..

Code:
#!/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:
Code:
ppend_desc='\<br\>\<br\>\
\n\<b\>alc131AIXqa199\<\/b\>\
\n\<br\>\<br\>\<a href=\"CLIENTURL br\>\&lt'


temp.txt:
Code:
client.url=http://google.com/images
wiki.url=


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

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



Best,
Vinodh Kumar
# 2  
Old 03-08-2011
I see what you mean now, it seems to remove the backslashes with sed thinking they are escape characters.. maybe use double backslashes?

Last edited by glev2005; 03-08-2011 at 02:00 PM..
# 3  
Old 03-08-2011
No. It doesn't work out. It assumes $URL as string value.

The output:

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

# 4  
Old 03-08-2011
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.
# 5  
Old 03-08-2011
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
Code:
sed -e 's%\/%\\\/%g'

should be
Code:
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:
Code:
sed -e 's%/%\\\\/%g'

Regards,
Alister

Last edited by alister; 03-08-2011 at 03:55 PM..
# 6  
Old 03-08-2011
How to deal with escaping in bash and sed

You can also simplify the URL variable assignment like so:
Code:
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

Last edited by freegnu; 03-08-2011 at 05:31 PM.. Reason: incorrect explanation
# 7  
Old 03-08-2011
Quote:
Originally Posted by freegnu
Code:
sed -e 's%/%\\\\\\\\/%g'

Makes it work.

Why?

sed needs triple escapes to escape a character i.e.
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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed Replace

I have a file whose output words are always like this: aaaa bbbb cccc dddd. Trying to arrange the data so that there are 2 columns such that the 1st word become the 1st column like this: aaaa aaaa aaaa bbbb aaaa cccc aaaa dddd Trying to use awk... (8 Replies)
Discussion started by: jimmyf
8 Replies

2. Shell Programming and Scripting

Sed/replace help

How can we empty or replace with null, following block of code (within the php quotes including the quotes) from inside a file. *** some other data above this code <? #317008# ... (5 Replies)
Discussion started by: fed.linuxgossip
5 Replies

3. Shell Programming and Scripting

sed replace

Hi, i have a file as give below >cat sample_file param1 val1 2012-06-19 ##there can be one or more space after 2012-06-19 in the above file i want to replace val1 with a with value passed through a variable... below is the command i tried >parval='param1 val2' >par1=param1 >sed... (3 Replies)
Discussion started by: midhun19
3 Replies

4. Shell Programming and Scripting

Help with sed replace

Hello, I have a comman separated file lets day data.txt in following format ,:000002 CH XIN9I.INDX, 34.7534909645,:000002 CH,:Index XIN9I.INDX ,:000063 CH XIN9I.INDX, 6.3062924781,:000063 CH,:Index XIN9I.INDX ,:000776 CH XIN9I.INDX, 2.7001954832,:000776 CH,:Index XIN9I.INDX I would like... (9 Replies)
Discussion started by: srattani
9 Replies

5. UNIX for Dummies Questions & Answers

SED Replace

I'm trying to change a "." in a file name with a "_" I have tried; sed -e 's/./_/g' However this then replaces the entire filename with a load of "_" For example; ls /usr/local/feed/service/customers/test1/configs/test1.httpsend | awk -F/ '{print $9}' | tr "" "" | sed -e "s/./_/g" ... (2 Replies)
Discussion started by: JayC89
2 Replies

6. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

7. Shell Programming and Scripting

using sed to replace help

Hi, i am following content in file cat file Install Installation-path variable Now i need to replace Installation-path with some text to be provided as argument in csh script invocation My question is , can i replace this by only using path eg. sed "s/path/$1" file but it... (1 Reply)
Discussion started by: sarbjit
1 Replies

8. UNIX for Dummies Questions & Answers

sed - replace $

my script: amount1=`tail /tmp/file1.txt` amount2=`tail /tmp/file2.txt` sed -e 's/'${amount2}'/'${amount1}'/g' filename1 > filename2 what did i do wrong ? i just want to replace amount1 with amount2 value. (2 Replies)
Discussion started by: tjmannonline
2 Replies

9. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

10. Shell Programming and Scripting

How to replace using SED?

Hi, I want to change a particular string in a file with another string. This is part of a larger script file. I m using SED for this purpose: sed -e 's/hostname.domainname/${HOST}.${DOMAIN}/g' $sed_file>$tmp_file Where the occurance hostname.domainname has to be replaced with the... (4 Replies)
Discussion started by: mahatma
4 Replies
Login or Register to Ask a Question