How to change a windows path to a linux path in all files under a directory using sed

StackOverflow https://stackoverflow.com/questions/23529669

  •  17-07-2023
  •  | 
  •  

Вопрос

I copied a directory structure from a windows box to a Linux box and I would like to use sed to replace c:\IBM\WebSphere with /opt/IBM/WebSphere in all files under this directory.

Any thoughts?

Это было полезно?

Решение

I think sed is a little inconvenient for that purpose, if you want to change the actual files you can use perl one-liner

perl -pi -e 's/c:\\IBM\\/\/opt\/IBM\//g' *

Add or adjust the paths according to what you need (add WebSphere if you want the replacement to change only these dirs)

Другие советы

Since sed can take any character as a delimiter, use

sed -e 's_\\_/_g'

to replace the \ to /.

sed -e 's_[Cc]:_/opt_g'

to replace the c: with /opt

You can string those together:

echo "C:\\IBM\\WebSphere" | sed -e 's_\\_/_g' -e 's_[Cc]:_/opt_g'

Output:

/opt/IBM/WebSphere

I don't see an awk solution, just add one:

awk -F'\' -v OFS='/' '$1=/^c:/?"/opt":$1'

test:

kent$  awk -F'\' -v OFS='/' '$1=/^c:/?"/opt":$1' <<<'c:\IBM\WebSphere'  
/opt/IBM/WebSphere

echo "C:\Users\San.Tan\My Folder\project1" | sed -e 's/C:\\/mnt\/c\//;s/\\/\//g'

replaces

C:\Users\San.Tan\My Folder\project1

to

mnt/c/Users/San.Tan/My Folder/project1

in case someone needs to replace windows paths to Windows Subsystem for Linux(WSL) paths

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top