问题描述
|
如何替换foo。与foo_和sed一起运行
sed \'s/foo./foo_/g\' file.php
不起作用。
谢谢!
解决方法
转义
.
:
sed \'s/foo\\./foo_/g\' file.php
例:
~$ cat test.txt
foo.bar
~$ sed \'s/foo\\./foo_/g\' test.txt
foo_bar
,有趣的是,如果您只想搜索和替换点,则必须将点放入字符集中。由于某种原因,仅在sed命令中仅转义点是无效的。点仍扩展为单个字符通配符。
$ bash --version # Ubuntu Lucid (10.04)
GNU bash版本4.1.5(1)-发行(x86_64-pc-linux-gnu)
$ echo aa.bb.cc | sed s/\\./-/g # replaces all characters with dash
\'-------- \'
$ echo aa.bb.cc | sed s/[.]/-/g # replaces the dots with a dash
aa-bb-cc
通过在搜索中添加首字符,转义有效。
$ echo foo. | sed s/foo\\./foo_/g # works
foo_
当然也可以将点放在字符集中。
$ echo foo. | sed s/foo[.]/foo_/g # also works
foo_
-
保罗
,用\\转义点
sed \'s/foo\\./foo_/g\' file.php
如果您发现/和\\的组合令人困惑,则可以使用另一个\'分隔符'
sed \'s#foo\\.#foo_#g
s之后的第二个字符可以是任何字符,sed将使用该字符作为分隔符。
,您需要转义该点-未转义的点将与foo之后的任何字符匹配。
sed \'s/foo\\./foo_/g\'
,对我来说,sed \'s/foo\\./foo_/g\'
正在工作。但您也可以尝试:
sed -e \'s/foo\\./foo_/g\'
和
sed -e \"s/foo\\\\./foo_/g\"
和
sed \'s/foo[.]/foo_/g\'