Operating System - HP-UX
1824976 Members
3696 Online
109678 Solutions
New Discussion юеВ

sed command to change /usr/local/bin to \/usr\/local\/bin

 
SOLVED
Go to solution
Jack C. Mahaffey
Super Advisor

sed command to change /usr/local/bin to \/usr\/local\/bin

I'm writing a script that will create a sed input file that will change all '/' to '\/'

Any ideas?

I tried s/\//\\\//g but no luck.

Jack...
10 REPLIES 10
Ralph Grothe
Honored Contributor

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

Have you tried the transliterate command tr yet.
I'd say something like

tr '\\' '/'

should work, but haven't given it a try myself yet.
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

sorry, you want it the other way round.
Just change the order of arguments to tr

e.g.

# pwd|tr '/' '\\'
\usr\local\sbin
Madness, thy name is system administration
Mark Grant
Honored Contributor
Solution

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

echo "/////" | sed 's/\//\\\//g'
Never preceed any demonstration with anything more predictive than "watch this"
Brian Bergstrand
Honored Contributor

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

Try this:

echo /usr/local/bin | | sed -e "s!/!\\\\/!g"

Using ! as the delimiter makes things a bit easier. Note that you have to output two backslashes in sed, becasue \ is also a shell escape char.

HTH.
Jean-Luc Oudart
Honored Contributor

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

try
echo "/usr/local/bin" | sed -e 's/\//\\\//g'

Rgds
JL
fiat lux
Sergejs Svitnevs
Honored Contributor

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

sed 's/\//\\\//g' file

Regards,
Sergejs
Jack C. Mahaffey
Super Advisor

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

Thanks all.


The tr command apparently only works for a single character. I wanted to change one character to two characters.

jack...
Kent Ostby
Honored Contributor

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

sed -e "s!\/!\\\/!g " < inputfile > outputfile

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Jack C. Mahaffey
Super Advisor

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

Now that it works on the command line I got another problem. It won't work when the command is in a sed input file.


I got a sed file that contains the following:
s/\//\\//g


If I use command withoutout the sed file it works, when I use the sed command file it fails with the following:
sed: "s/\//\\//g" is not a recognized function.


any ideas for this twist?
Jack C. Mahaffey
Super Advisor

Re: sed command to change /usr/local/bin to \/usr\/local\/bin

Problem solved. Replaced / delimiter with !

e.g. s!\/!\\/!g

Thanks to all...