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