LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 01-19-2004, 07:44 AM   #1
stelmed
Member
 
Registered: Mar 2003
Location: Evia-Greece
Distribution: Slackware
Posts: 104

Rep: Reputation: 15
sed command to replace slash with backslash


Could someone provide me with the right command needed in order for sed to replace slash with backslash?
The default syntax "sed s/replace_this//with_this/g will work fine with letters but not with special charechters such as \ or /.

Than you in advance!
 
Old 01-19-2004, 12:03 PM   #2
poison
Member
 
Registered: Dec 2003
Location: Layer 7 =D
Distribution: Slackware, LFS, Rock Linux
Posts: 165

Rep: Reputation: 30
did you try to escape those / and \ ?
like \/ for a slash and \\ for a backslash ?
 
Old 01-19-2004, 05:42 PM   #3
magicm
Member
 
Registered: May 2003
Distribution: Slackware
Posts: 237

Rep: Reputation: 152Reputation: 152
poison has the key - however, the kicker is that this is very awkward to do straight from the command line because the shell will want to get in and parse the escape sequences in ways and times that will get in the way. However, if you store the sed commands in a file, it is quite simple.
For example, create a 1 line file namede "front2back.sed" containing
Code:
s/\//\\/g
then you can invoke sed like so:
Code:
sed -f front2back.sed < file2change.txt > newfile.txt
It will take all the forward slashes in file2change.txt, and make them back slashes in newfile.txt
Oh, - and see http://sed.sourceforge.net/sedfaq.html
 
Old 01-20-2004, 10:05 AM   #4
stelmed
Member
 
Registered: Mar 2003
Location: Evia-Greece
Distribution: Slackware
Posts: 104

Original Poster
Rep: Reputation: 15
Thanx for the help, it worked!

Really thank you guys!
 
Old 05-27-2008, 07:40 PM   #5
baltho
Member
 
Registered: Aug 2004
Location: Adelaide, South Australia
Distribution: Ubuntu (Natty)
Posts: 38

Rep: Reputation: 18
And one more thing

Hi all,

I know, this is way too late but hopefully will help someone (like myself) who wants the command-line version to work instead.
I needed to double-backslash the contents of a file (SQL Insert statements, if you're wondering) in a command pipeline, looked here, kept on fiddling, and found that the simple solution is to use single rather than double quotes, which stops the shell from interpreting the contents of the sed command.

echo some-file.sql | sed -e 's/\\/\\\\/g' >some-other-file.sql

did the trick for me.
 
Old 05-27-2008, 09:36 PM   #6
jong357
Senior Member
 
Registered: May 2003
Location: Columbus, OH
Distribution: DIYSlackware
Posts: 1,914

Rep: Reputation: 52
Quote:
Originally Posted by baltho View Post
the simple solution is to use single rather than double quotes, which stops the shell from interpreting the contents of the sed command.
You got it. That's the main reason for using double quotes.

Code:
[root@jaguar ~/Desktop] variable=blah
[root@jaguar ~/Desktop] echo $variable
blah
[root@jaguar ~/Desktop] echo $variable | sed 's/$variable/blek/'
blah
[root@jaguar ~/Desktop] echo $variable | sed "s/$variable/blek/"
blek
 
Old 05-28-2008, 05:26 AM   #7
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,910

Rep: Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026
I know you asked specifically about sed, but with Unix, it's often better to ask "how to do x" rather than "how to do x using y", unless you specifically need to use 'y' that is. Quite often there are better alternatives.


For a simple replacement such as you describe 'tr' might suit you better and would possibly be more efficient to run.

Code:
tr '\' '/' <input.file >output.file

If you need to use sed because you're doing something more complex than that, then a little known tip is that you can actually use other delimiters to make it more readable. Though you still need to escape the '\' to stop sed misinterpreting it, you won't need to escape the '/'

I'm sure you'll agree that

Code:
sed -e 's:\\:/:g' input.file
is much clearer than

Code:
sed -e 's/\\/\//g' input.file

You've already had some answers so you've probably sorted this out, but I thought I'd share this anyway. Anyone who ever has to look at your code in future would thank you for using either of these techniques.

edit: oops, just noticed this was a revived thread. That'll teach me to look at the dates more closely. Ahh well, Maybe someone will find it useful if they stumble upon it.

Last edited by GazL; 06-12-2011 at 04:54 AM.
 
Old 07-09-2010, 12:54 PM   #8
GyulaCsom
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Rep: Reputation: 0
2 years later

I should tell that have found the tip on custom delimiters useful That is when dealing with file path expressions in sed. For instance to dynamically remove an iSCSI LUN from ietd.conf one could simple put the following or kinda:

Code:
su -c "cat /etc/ietd.conf | sed -e \"s:Lun $LUN Path=$LUN_DEV,Type=blockio::g\" > /tmp/ietd.conf"
su -c "cp /tmp/ietd.conf /etc"
Without the custom delimiter the path expression '$LUN_DEV' would generate a sed error message...
 
Old 07-30-2010, 05:04 AM   #9
tomaski2001
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Rep: Reputation: 0
share file path with pc users

is there a possibility to use a plugin to change file paths in mac into pc format? this means backslash has to change into fslash. I don't want to do it manually because it's about lot's of files veryday (work related).
Mainly i want to select a path (in file info) and copy it in a mail or a document with chanched slashes. Nobody finds a way.

Maybe here?
 
Old 07-30-2010, 06:44 AM   #10
vst
LQ Newbie
 
Registered: Jul 2010
Location: Ukraine
Distribution: Slackware
Posts: 20

Rep: Reputation: 3
tomaski2001,

have you actually tried sending windows users the paths with forward slashes? Thing is, forward slashes work in windows the same way, as backslashes do. Try it.
 
Old 07-30-2010, 07:10 AM   #11
tomaski2001
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Rep: Reputation: 0
i tried it...

but it didn't work.
I also want to make it a hyperlink to a server location, but that also is not working.
 
Old 07-31-2010, 04:16 PM   #12
GyulaCsom
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Rep: Reputation: 0
File URLs in Windows

Quote:
Originally Posted by tomaski2001 View Post
but it didn't work.
It should That is Windows should honor file URLs, just as vst put. Try for instance the following sample urls:

Local file URL

Code:
file:///c:/tmp
If you put it into Windows Explorer it should open your local tmp folder.

Server file URL
Quote:
Originally Posted by tomaski2001 View Post
I also want to make it a hyperlink to a server location, but that also is not working.
Put something like this:

Code:
file://yourserver/sharedfolder
If you replace 'yourserver' and 'sharedfolder' with a server and folder you have access to windows explorer should open it.

More info
See Wikipedia for more details: File URI scheme

Hope it helps,

Cheers,
Gyula
 
Old 08-18-2010, 07:28 AM   #13
Insulaner
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
i want to escape the dots of an url so i wrote the following code:
Code:
url="google.de"
echo $url	# output: google.de
escapedurl="`echo $url | sed 's:\.:\\\.:g'`"
echo $escapedurl	# output: google\.de
# content of /var/www/template :
# {url}
# {escapedurl}
sed "s:{url}:$url:g" /var/www/template | sed "s:{escapedurl}:$escapedurl:g"
# output:
# google.de
# google.de
the last output should be the escaped url but it doesn't work. the backslash is ignored. i guess it's a problem with the quotation marks. i also tried single quotes but then the variable is not interpreted.
so can anybody tell me how to fix this?
 
Old 08-19-2010, 05:01 AM   #14
Insulaner
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
i got it, not enough backslashes:
Code:
escapedurl="`echo $url | sed 's:\.:\\\\\\\.:g'`"
 
  


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 can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
Replace substring with SED marri Programming 2 07-09-2005 05:18 PM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
Sed - suitable to replace CR LF? J_Szucs Programming 3 05-12-2003 06:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 04:07 AM.

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