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! |
You need to escape the backslash - keep adding them (try 2, then maybe 3 depending on circumstance).
|
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.
|
Try
Code:
echo "/home/project/testfile" | sed 's:/:\\/:g' |
Thanks so much! That worked perfectly.
|
All times are GMT -5. The time now is 10:42 AM. |