I have a file "test.txt" that contain the following
+foo+
+bar+
What I want to do is to replace them into:
'foo'
'bar'
But why this code doesn't work?
sed 's/\+/\'/' test.txt
What's the right way to do it?
Use " instead. And add g flag to replace all. sed "s/\$/\'/g" ut. txt - this is adding ' at the end instead replacing the $ character.
How SED Works. In the syntax, you only need to provide a suitable “new string” name that you want to be placed with the “old string”. Of course, the old string name needs to be entered as well. Then, provide the file name in the place of “file_name” from where the old string will be found and replaced.
The s command (as in substitute) is probably the most important in sed and has a lot of different options. The syntax of the s command is ' s/ regexp / replacement / flags '.
Use "
instead. And add g
flag to replace all.
sed "s/\+/\'/g" test.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