简体   繁体   中英

How to replace '/' with '/\' using sed in shell scripting

I am trying to replace

prakash/annam/DevOps ---> prakash/\\annam/\\Devops

I am using this:

sed "s/'[//]''///\\/g"

Unfortunately, it is not giving the required output can anyone please help with this!!!

you can use a separator other than slash:

$ sed 's#/#\\/#g' <<< "a/b/c"
a\/b\/c

$ sed 's#/#/\\#g' <<< "a/b/c"
a/\b/\c

You can use sed with -i flag to place in place changes to the file

*nix

$ cat test 
prakash/annam/DevOps

$ sed -i 's/\//\/\\/g' test 

$ cat test
prakash/\annam/\DevOps

MacOS

$ cat test 
prakash/annam/DevOps

$ sed -i '' 's/\//\/\\/g' test 

$ cat test
prakash/\annam/\DevOps

Use

sed -E 's/\//\/\\/g'

eg

$ echo "prakash/annam/DevOps" | sed -E 's/\//\/\\/g'
prakash/\annam/\DevOps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM