我有一个文件“test.txt”,其中包含以下内容
+foo+ +bar+
我想要做的是把它们replace成:
'foo' 'bar'
但为什么这个代码不起作用?
sed 's/\+/\'/' test.txt
什么是正确的方法来做到这一点?
使用"
代替,并添加克旗替换所有。
sed "s/\+/\'/g" test.txt
这可能适合yoyu(GNU sed):
sed 'y/+/'\''/' file
您也可以使用tr
替换文件中的所有+
实例:
tr '+' "'" < inputfile
+
不是在sed中没有-r
开关的特殊字符。 你可以运行substitute命令而不用任何转义 :
echo '+foo+' | sed "s/+/'/g" # output: 'foo'
如果你想保存更改的文件,然后使用:
sed -i.bak "s/+/'/g" test.txt