Ian Vaughan
Ian Vaughan

Reputation: 21321

Converting between windows and unix path with sed losses double slash

This command almost gives me what I want :-

echo "\\123.123.123.123\path1\1 - path2\path3 path4\path5" | sed 's_\\_/_g' | sed 's_ _\\ _g'
/123.123.123.123/path1/1\ -\ path2/path3\ path4/path5

But as one can see, its lost the '//' at the beginning!
ie, desired outout :-

//123.123.123.123/path1/1\ -\ path2/path3\ path4/path5

What am I missing?


Edit: After not testing the basics of echo (many thanks to all who pointed that out). I should of also been more clear on the end-game of this question.

I want to use this in a script, and define the windows path at the top.
How can I echo the path to a tmp file for sed?
This obviously wont work :-

WIN_PATH="\\123.123.123.123\path1\1 - path2\path3 path4\path5"
UNIX_PATH=`echo $WIN_PATH | sed 's_\\_/_g' | sed 's_ _\\ _g'`

Upvotes: 3

Views: 4050

Answers (4)

kurumi
kurumi

Reputation: 25599

if you want to do that, use single quotes so that the slash does not get interpreted.

echo '\\123.123.123.123\path1\1 - path2\path3 path4\path5'

Upvotes: 1

odrm
odrm

Reputation: 5259

Bash uses the \ (backslash) as an escape. It isn't being munched by sed, but by bash before passing the string to echo. Try:

echo "\\123"

You'll get:

\123

To solve your problem, put your text into a file, and read it from there to avoid shell escaping:

$ cat >file
\\123.123.123.123\path1\1 - path2\path3 path4\path5
$ cat file | sed 's_\\_/_g' | sed 's_ _\\ _g'
//123.123.123.123/path1/1\ -\ path2/path3\ path4/path5

Upvotes: 1

thkala
thkala

Reputation: 86333

Use single quotes instead of double quotes in echo - as it is the shell interprets your two backslashes as an escape sequence for \:

$ echo '\\123.123.123.123\path1\1 - path2\path3 path4\path5' | sed 's_\\_/_g' | sed 's_ _\\ _g'
//123.123.123.123/path1/1\ -\ path2/path3\ path4/path5

Using single quotes suppresses shell expansions (e.g. variables) and disables most escape sequences.

Upvotes: 4

pconcepcion
pconcepcion

Reputation: 5641

Your problem is that in \\ is already the escape secuence for \ so you have already lost one of the \ on the echo. You can try just :

echo "\\123.123.123.123\path1\1 - path2\path3 path4\path5" 

to see it.

You have to escape those \ to make it work i.e.:

echo "\\\\123.123.123.123\path1\1 - path2\path3 path4\path5" | sed 's_\\_/_g' | sed 's_ _\\ _g'
//123.123.123.123/path1/1\ -\ path2/path3\ path4/path5

Upvotes: 2

Related Questions