I have a string in which I need to replace "," with "\," using shell script. I thought I can use sed
to do this but no luck.
You can do that without sed
:
string="${string/,/\\,}"
To replace all occurrences of "," use this:
string="${string//,/\\,}"
Example:
#!/bin/bash
string="Hello,World"
string="${string/,/\\,}"
echo "$string"
Output:
Hello\,World
You need to escape the back slash \/
I'm not sure what your input is but this will work:
echo "teste,test" |sed 's/,/\\/g'
output:
teste\test
Demo: http://ideone.com/JUTp1X
If the string is on a file, you can use:
sed -i 's/,/\//g' myfile.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With