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?
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" test.txt
+
is not a special character without -r
switch in sed. You can run the substitute command without any escaping:
echo '+foo+' | sed "s/+/'/g"
# output: 'foo'
If you want to save changed file then use:
sed -i.bak "s/+/'/g" test.txt
You can also replace all instances of +
with '
in the file by using tr
:
tr '+' "'" < inputfile