f



how to change "/" to "\"

iam new to shell scripting  and i have plz can anyone help in changing
the pattrern "/" to "\" using the sed command.
0
olympie (1)
1/28/2009 7:07:42 AM
comp.unix.questions 2631 articles. 0 followers. Post Follow

9 Replies
991 Views

Similar Articles

[PageSpeed] 19

olympie@gmail.com wrote:
> iam new to shell scripting  and i have plz can anyone help in changing
> the pattrern "/" to "\" using the sed command.

sed 's/\//\\/g' will replace all '/' with '\'

srp
-- 
http://saju.net.in
0
1/28/2009 7:18:36 AM
Saju Pillai <saju.pillai@gmail.com> wrote:
>> iam new to shell scripting  and i have plz can anyone help in changing
>> the pattrern "/" to "\" using the sed command.
> sed 's/\//\\/g' will replace all '/' with '\'

For less confusing viewing:

sed 's,/,\\,g'

For original poster: backslash is "quoting" character so if you need
literal, you need it quoted by itself; previous poster had to also
quote forward slash because it was used as delimiter in sed commands.

p.

-- 
Beware of he who would deny you access to information, for in his
heart he dreams himself your master.   -- Commissioner Pravin Lal
0
chopin (4)
1/28/2009 9:55:53 PM
2009-01-27, 23:07(-08), olympie@gmail.com:
> iam new to shell scripting  and i have plz can anyone help in changing
> the pattrern "/" to "\" using the sed command.

Why sed?

tr / '\\'

-- 
St�phane
0
1/29/2009 6:07:22 PM
According to Saju Pillai  <saju.pillai@gmail.com>:
:olympie@gmail.com wrote:
:> iam new to shell scripting  and i have plz can anyone help in changing
:> the pattrern "/" to "\" using the sed command.
:
:sed 's/\//\\/g' will replace all '/' with '\'
:
:srp
:-- 
:http://saju.net.in

 or 

 sed 's#/#\#g'

 richk


0
richk1 (6)
1/29/2009 6:19:29 PM
Stephane CHAZELAS wrote:
> 2009-01-27, 23:07(-08), olympie@gmail.com:
>> iam new to shell scripting  and i have plz can anyone help in changing
>> the pattrern "/" to "\" using the sed command.
> 
> Why sed?
> 
> tr / '\\'
> 

or simply turn your monitor 90 degrees to the left :-/

I found that shells treat \ in '' differently.
Some need '\' and some '\\':

Safer is:

tr / \\


-- 
echo imhcea\.lophc.tcs.hmo |
sed 's2\(....\)\(.\{5\}\)2\2\122;s1\(.\)\(.\)1\2\11g;1s;\.;::;2'
0
eedmit (141)
1/29/2009 6:50:31 PM
2009-01-29, 19:50(+01), Michael Tosch:
> Stephane CHAZELAS wrote:
>> 2009-01-27, 23:07(-08), olympie@gmail.com:
>>> iam new to shell scripting  and i have plz can anyone help in changing
>>> the pattrern "/" to "\" using the sed command.
>> 
>> Why sed?
>> 
>> tr / '\\'
>> 
>
> or simply turn your monitor 90 degrees to the left :-/
>
> I found that shells treat \ in '' differently.
> Some need '\' and some '\\':
>
> Safer is:
>
> tr / \\
[...]

No, there's no ambiguity here, you want:

tr / '\\'

whatever the shell (Bourne, rc or csh families).

Or in Bourne and csh families
tr / \\\\

Or in Bourn family:

tr / "\\\\"

Or in csh family:

tr / "\\"

tr's behavior for:
tr / '\'
(or tr / \\)
is unspecified, you need to double the \ for tr.

See SUSv4:
http://www.opengroup.org/onlinepubs/9699919799/utilities/tr.html
for details.

-- 
St�phane
0
1/29/2009 7:11:48 PM
richk@panix.com (Richard T. Kus) wrote:

> sed 's#/#\#g'

   $ echo /usr/bin/sh | sed 's#/#\#g'
   sed: -e expression #1, char 7: unterminated `s' command

If I were to use sed, I would use a double backslash.

Peter

-- 
~/.signature: No such file or directory
0
pjacklam (309)
1/29/2009 7:12:05 PM
Stephane CHAZELAS wrote:
> 2009-01-29, 19:50(+01), Michael Tosch:
>> Stephane CHAZELAS wrote:
>>> 2009-01-27, 23:07(-08), olympie@gmail.com:
>>>> iam new to shell scripting  and i have plz can anyone help in changing
>>>> the pattrern "/" to "\" using the sed command.
>>> Why sed?
>>>
>>> tr / '\\'
>>>
>> or simply turn your monitor 90 degrees to the left :-/
>>
>> I found that shells treat \ in '' differently.
>> Some need '\' and some '\\':
>>
>> Safer is:
>>
>> tr / \\
> [...]
> 
> No, there's no ambiguity here, you want:
> 
> tr / '\\'
> 
> whatever the shell (Bourne, rc or csh families).
> 
> Or in Bourne and csh families
> tr / \\\\
> 
> Or in Bourn family:
> 
> tr / "\\\\"
> 
> Or in csh family:
> 
> tr / "\\"
> 
> tr's behavior for:
> tr / '\'
> (or tr / \\)
> is unspecified, you need to double the \ for tr.
> 

You are right, tr wants \\, I didn't even check that :-(

And what confused me:

bash in Solaris:
echo '\\'
\\

ksh and /bin/sh in Solaris:
echo '\\'
\

Now I see it's not the shell but the echo command
that makes the difference.

printf "%s\n" '\\'

behaves equally.

-- 
echo imhcea\.lophc.tcs.hmo |
sed 's2\(....\)\(.\{5\}\)2\2\122;s1\(.\)\(.\)1\2\11g;1s;\.;::;2'
0
eedmit (141)
1/29/2009 7:58:16 PM
Michael Tosch <eedmit@NO.eed.SPAM.ericsson.PLS.se> wrote:

> And what confused me:
>
> bash in Solaris:
> echo '\\'
> \\
>
> ksh and /bin/sh in Solaris:
> echo '\\'
> \
>
> Now I see it's not the shell but the echo command
> that makes the difference.

It depends on whether 'echo' is /usr/bin/echo or /usr/ucb/echo.
I don't remember which does what, but one of them converts
backslashed characters, like

   printf '%b' $*

whereas the other is like

   printf '%s' $*

Both are described in the manual page for 'echo'.

> printf "%s\n" '\\'
>
> behaves equally.

Really? I tried both bash's printf and Solaris' /usr/bin/printf
at work today, and in both cases did

   printf '%s\n' '\\'

give

   \\

Peter

-- 
~/.signature: No such file or directory
0
pjacklam (309)
1/30/2009 5:30:23 PM
Reply: