Ranjit
Ranjit

Reputation: 145

Using sed command how can I replace Linux path to Windows path

I want to change the Linux path to a Windows Path, using "sed" command, for example:

Linux path: /opt/test/dash/apps/tomcat to Windows Path: c:\\test\\dash\\apps\\tomcat

I tried with:

sed -i 's|'/opt/test/dash/apps/tomcat'|'c:\\\\\\\test\\\\\\\dash\\\\\\\apps\\\\\\\tomcat'|g' /filename - But no luck!!

What I exactly want all /opt/ should replace by c:\\ and rest of the "/" should be replace by "\\".

NOTE: I am executing this command remotely using ssh2_exec, All "sed" commands are working except the above.

Thanks in advance!!

Upvotes: 0

Views: 2740

Answers (2)

Raman Sailopal
Raman Sailopal

Reputation: 12877

I'd use regular expressions and so:

sed -r 's@/(.*)/(.*)/(.*)/(.*)/(.*)@C:\\\\\2\\\\\3\\\\\4\\\\\5@'

Using -r to enable interpretation of regular expressions and @ as the sed separator, split the path in 5 parts and then refer to them in the translated section with \1 \2 etc.

Upvotes: 0

fredtantini
fredtantini

Reputation: 16556

I would do it in two steps:

$>echo '/opt/test/dash/apps/tomcat' | sed 's#/opt#c:#g'|sed 's#/#\\\\#g'
c:\\test\\dash\\apps\\tomcat

First changing the /opt with c:, then change the / with \ that you have to escape

Upvotes: 1

Related Questions