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 - 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, 05: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, 06:06 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
You need to escape the backslash - keep adding them (try 2, then maybe 3 depending on circumstance).
 
Old 08-14-2009, 06: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, 06:31 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 11:32 AM   #5
cg97330
LQ Newbie
 
Registered: Aug 2009
Posts: 3

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


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:08 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