f



replacing '/' with '\\'

I have a MKS ksh shell script running on a Windows box. In some scripts
I have to convert a UNIX filename into a DOS filename, that is replace
the '/' with  '\\'.  For example, replace /opt/siips/archive/text.dat
with \\opt\\siips\\archive\\text.dat.
I've tried using sed like $FILE='echo $FILE|sed "s/\//\\\/g"' which
got me \opt\siips\archive\text.dat. It's close but I still need the
second \.

Does anyone have any suggestion? Or tell me where I've gone wrong?
Thanks

-- Steffen

0
sajohn52
12/10/2004 4:14:21 AM
comp.unix.shell 15484 articles. 2 followers. Post Follow

4 Replies
841 Views

Similar Articles

[PageSpeed] 38

On 9 Dec 2004 20:14:21 -0800, sajohn52@yahoo.com
<sajohn52@yahoo.com> wrote:


> I have a MKS ksh shell script running on a Windows
> box. In some scripts I have to convert a UNIX filename
> into a DOS filename, that is replace the '/' with '\\'.
> For example, replace /opt/siips/archive/text.dat with
> \\opt\\siips\\archive\\text.dat.  I've tried using sed
> like $FILE='echo $FILE|sed "s/\//\\\/g"' which got me
> \opt\siips\archive\text.dat. It's close but I still need the
> second \.
>
> Does anyone have any suggestion? Or tell me where I've gone
> wrong?  Thanks
>
> -- Steffen
>

echo "aaa/bbb/ccc" | sed 's:/:\\\\:g'

Those'll drive you nuts. 

:-)

AC

0
Alan
12/10/2004 4:27:35 AM
sajohn52@yahoo.com wrote:
> I have a MKS ksh shell script running on a Windows box. In some
scripts
> I have to convert a UNIX filename into a DOS filename, that is
replace
> the '/' with  '\\'.  For example, replace /opt/siips/archive/text.dat
> with \\opt\\siips\\archive\\text.dat.
> I've tried using sed like $FILE='echo $FILE|sed "s/\//\\\/g"' which
> got me \opt\siips\archive\text.dat. It's close but I still need the
> second \.
>
> Does anyone have any suggestion? Or tell me where I've gone wrong?
> Thanks
>
> -- Steffen

try adding one more \

\ -- escape, the second \ - is for your path, the third \ is for
escape, the fourth for your second \.

-Moo

0
moo_glee
12/10/2004 7:12:13 AM
On 12/10/04 05:14 AM, sajohn52@yahoo.com wrote:
> I have a MKS ksh shell script running on a Windows box. In some scripts
> I have to convert a UNIX filename into a DOS filename, that is replace
> the '/' with  '\\'.  For example, replace /opt/siips/archive/text.dat
> with \\opt\\siips\\archive\\text.dat.
> I've tried using sed like $FILE='echo $FILE|sed "s/\//\\\/g"' which
> got me \opt\siips\archive\text.dat. It's close but I still need the
> second \.
> 
> Does anyone have any suggestion? Or tell me where I've gone wrong?
> Thanks
> 
> -- Steffen
> 

s,/,\\\\,g

It just look very complicated since you are using / as both
delimiter and string to replace.

/bb
0
Birger
12/10/2004 11:29:38 AM
sajohn52@yahoo.com wrote:
> I have a MKS ksh shell script running on a Windows box. In some
scripts
> I have to convert a UNIX filename into a DOS filename, that is
replace
> the '/' with  '\\'.  For example, replace /opt/siips/archive/text.dat
> with \\opt\\siips\\archive\\text.dat.
> I've tried using sed like $FILE='echo $FILE|sed "s/\//\\\/g"' which
> got me \opt\siips\archive\text.dat. It's close but I still need the
> second \.
>


Go for 'printf' or the here-document operator << rather than the
'echo'.

printf '%s\n' "$FILE"
NFILE=`printf '%s\n' "$FILE" | sed -e 's:/:\\\\\\\\:g'` # 8 backslashes
echo "echo   -> $NFILE"
printf 'printf -> %s\n' "$NFILE"

Accounting for the \s ->
out of 8:
8/2 = 4 consumed by 'sed'
out of those remaining 4:
4/2 = 2 consumed by the `...` operator
out of those remaining 2:
passed onto echo/printf.

used as it is by the 'printf' since it
doesn't consider \ as special.

In case of the 'echo', however, it is implementation-dependent.

some 'echos' treat \ specially, i.e., \\ -> \
Hence the 2 \s that echo recevies shall be truncated to 1 \
To see the 2 \s change the 8 \s to 16 but run the risk of
being non-portable or use either printf/here-document <<

cat - << [EOF]
$NFILE
[EOF]

will preserve the backslashes just like printf.

0
Rakesh
12/10/2004 12:10:17 PM
Reply: