0

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!!

jww
  • 97,681
  • 90
  • 411
  • 885
Ranjit
  • 135
  • 7
  • `sed` will butcher end-of-line. It converts them to Linux `LF` even on Windows where `CR-LF` is used. You should run `unix2dos.exe` to repair them after running `sed` on your source file. – jww Jul 08 '17 at 12:15
  • Possible duplicates: [How to change a windows path to a linux path in all files under a directory using sed](https://stackoverflow.com/q/23529669/608639), [Bash script to convert windows path to linux path](https://stackoverflow.com/q/19999562/608639), etc. – jww Jul 08 '17 at 12:19

2 Answers2

1

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

fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • Hi Thanks, but how can I apply the same command to a group of files? Like: sed 's#/opt#c:#g'|sed 's#/#\\\\#g' *.properties – Ranjit Jul 08 '17 at 11:48
  • I would use a for loop: `for i in *properties; do sed -i 's#/opt#c:#g'|sed 's#/#\\\\#g' $i;done` – fredtantini Jul 08 '17 at 11:54
0

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.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • Thanks, but how can I execute the same command to change the contents inside the file, Like : sed -r 's@/(.*)/(.*)/(.*)/(.*)/(.*)@C:\\\\\2\\\\\3\\\\\4\\\\\5@' – Ranjit Jul 08 '17 at 11:55
  • Working with the following command sed -i -r 's@/(.*)/(.*)/(.*)/(.*)/(.*)@C:\\\\\2\\\\\3\\\\\4\\\\\5@' *.properties – Ranjit Jul 08 '17 at 12:00