If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > substitute "\" with "\\"

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-06-03, 21:03
rachana rachana is offline
Registered User
 
Join Date: Oct 2003
Posts: 3
substitute "\" with "\\"

Hi,

I need to subsititue a "\" with "\\"

I tried this:
$ print -r $abc
\some\var
$ res=`print -r $abc | sed -e 's#\\#\\\\#g' `
sed: Unterminated `s' command

the backticks and ' are both balanced but it still gives the error. Any ideas?

thanks,
Rachana.
Reply With Quote
  #2 (permalink)  
Old 10-06-03, 22:31
rachana rachana is offline
Registered User
 
Join Date: Oct 2003
Posts: 3
Re: substitute "\" with "\\"

Quote:
Originally posted by rachana
Hi,

I need to subsititue a "\" with "\\"

I tried this:
$ print -r $abc
\some\var
$ res=`print -r $abc | sed -e 's#\\#\\\\#g' `
sed: Unterminated `s' command

the backticks and ' are both balanced but it still gives the error. Any ideas?

thanks,
Rachana.
this works :

# replace the "\" with "/"
$res=`print -r $abc | tr '\\\\' /`
## thats 4 "\"'s in the tr but when i post it one is gone??
# replace "/" with "\\"
$res=`print -r $res | sed -e 's#\/#\\\\\\\\#g'`

did someone figure out a more elegant solution?

thanks,
Rachana

Last edited by rachana; 10-06-03 at 22:34.
Reply With Quote
  #3 (permalink)  
Old 10-07-03, 04:27
pooja pooja is offline
Registered User
 
Join Date: Dec 2002
Posts: 104
Re: substitute "\" with "\\"

Quote:
Originally posted by rachana
this works :

# replace the "\" with "/"
$res=`print -r $abc | tr '\\\\' /`
## thats 4 "\"'s in the tr but when i post it one is gone??
# replace "/" with "\\"
$res=`print -r $res | sed -e 's#\/#\\\\\\\\#g'`

did someone figure out a more elegant solution?

thanks,
Rachana
hi!

try this command...........

sed -e 's/\\/\\\\/g' filename > newfilename

hope this will work for u.

--pooja
Reply With Quote
  #4 (permalink)  
Old 10-07-03, 16:54
rachana rachana is offline
Registered User
 
Join Date: Oct 2003
Posts: 3
Re: substitute "\" with "\\"

Quote:
Originally posted by pooja
hi!

try this command...........

sed -e 's/\\/\\\\/g' filename > newfilename

hope this will work for u.

--pooja
it doesnt:
$abc='\some\var\'
$ res=`print -r $abc | sed -e 's/\\/\\\\/'`
sed: command garbled: s/\/\\/

but I tried this and it works:

$res=`print -r $abc | sed -e 's#\\\\#\\\\\\\\#g'`
+ + print -r \12\13\
+ sed -e s#\\#\\\\#g
res=\\12\\13\\

Needed to escape the backslash twice, once for the shell and then again for sed because i am taking the variable from the print stmt instead of a file.

Thanks,
Rachana.
Reply With Quote
  #5 (permalink)  
Old 10-17-03, 03:08
karthi_tvr karthi_tvr is offline
Registered User
 
Join Date: Oct 2003
Location: Singapore
Posts: 12
Thats correct. You need to do the escaping twice. Else you can put the string in a file and use -f with sed.
__________________
Thanks and Regards
Karthik R
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On