Saiprakash annam.a
Saiprakash annam.a

Reputation: 53

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

Upvotes: 1

Views: 81

Answers (3)

L. Monty
L. Monty

Reputation: 882

Use

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

e.g.

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

Upvotes: 1

Vaebhav
Vaebhav

Reputation: 5052

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

Upvotes: 1

Kent
Kent

Reputation: 195209

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

Upvotes: 1

Related Questions