LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-14-2009, 06:36 PM   #1
cg97330
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Rep: Reputation: 0
Trying to replace '/' with '\/' in files using bash


Hi,

I'm using bash and am trying to find all / and replace with \/ in a line of text.

Basically, I want

/home/project/testfile

to become

\/home\/project\/testfile

I've tried many iterations of

sed -i s/'/'/'\/'/ testfile.dat

using single quotes, double quotes, and "\" to no avail. Any suggestions?

Thank you!
 
Old 08-14-2009, 07:06 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,834

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
You need to escape the backslash - keep adding them (try 2, then maybe 3 depending on circumstance).
 
Old 08-14-2009, 07:24 PM   #3
cg97330
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Hmm, I can't seem to get it to work no matter how many I add. I was able to get around it using awk by changing the FS to /, and then printing each field with a \/ separator. I don't want to do it that way though, because I won't always know how many fields I have, so I'd have to write a loop.
 
Old 08-14-2009, 07:31 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,834

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Try
Code:
echo "/home/project/testfile" | sed 's:/:\\/:g'
The colon as separator is merely for readability - the manpage will explain the other change.
 
Old 08-17-2009, 12:32 PM   #5
cg97330
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks so much! That worked perfectly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 02:14 PM
Bash search and replace utility cpedros Programming 3 03-12-2008 02:02 PM
Search and Replace from bash hammertime1983 Linux - Newbie 4 11-02-2007 04:18 PM
Bash - txt replace clpl1980 Fedora 2 01-22-2007 06:44 AM
howto replace bash ? pingvina Linux - Software 12 12-30-2005 03:21 PM


All times are GMT -5. The time now is 01:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration