LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2011, 07:34 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
Replacing "slashes" with back slashes - escape character problem


Usually if I have to replace a character in a string I used the sed /s/ command. However, I am having some difficulty in doing the same thing when I have the following string in a variable in my shell script and I need to replace all the forward slashes ("/") to backslashes ("\"). For example, this is what I am doing:
Code:
URL=<\\\\software\\projects\\new development\\code\PartA/release/DEV-0.01_RC1/>
echo $URL | sed 's/\/\/g'
sed: -e expression #1, char 7: unterminated `s' command
Not sure how to escape the slashes in this case.
 
Old 01-07-2011, 07:48 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Code:
 echo $URL | sed 's/\//\\/g'
(a) / and \ are both special in regular expressions; you need to escape both. With spaces (for clarity; this will break the substitution): s/ \/ / \\ /g
(b) Not sure how you meant your original replace to work, but how I read it, you were trying to replace /s with /s.
Code:
$ echo $URL | sed 's/\//\\/g'
<\\software\projects\new development\code\PartA\release\DEV-0.01_RC1\>

Last edited by AlucardZero; 01-07-2011 at 07:50 PM.
 
Old 01-08-2011, 01:16 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
To make things a little clearer to you can also change the 's' delimiter:
Code:
$ echo $URL | sed 's@/@\\@g'
This also has the added bonus of not needing to escape the forward slash
 
1 members found this post helpful.
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to escape "at" character in the username of an ftp url s.villanueva Linux - General 5 11-23-2011 03:48 PM
Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!) d1s4st3r Programming 5 09-29-2010 09:30 PM
"Sorry for the slashes" Jeebizz Linux - News 0 10-16-2009 08:24 AM
bash and double slashes MD3 Linux - Software 0 02-18-2005 03:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration