当存在点和下划线时,使用sed搜索并替换

时间:2021-09-11 08:43:01

How do I replace foo. with foo_ with sed simply running

我该如何替换foo。用foo_和sed一起运行

sed 's/foo./foo_/g' file.php

doesn't work. Thanks!

不起作用。谢谢!

5 个解决方案

#1


18  

Escape the .:

逃离。:

sed 's/foo\./foo_/g' file.php

sed's / foo \ ./ foo_ / g'file.php

Example:

例:

~$ cat test.txt 
foo.bar
~$ sed 's/foo\./foo_/g' test.txt 
foo_bar

#2


6  

Escape the dot with a \

用一个\逃脱点

sed 's/foo\./foo_/g' file.php

If you find the combination of / and \ confusing you can use another 'separator' character

如果您发现/和\ confusing的组合,您可以使用另一个'分隔符'字符

sed 's#foo\.#foo_#g

The 2nd character after the s can be anything and sed will use that character as a separator.

s之后的第二个字符可以是任何东西,sed将使用该字符作为分隔符。

#3


6  

Interestingly, if you want to search for and replace just the dot, you have to put the dot in a character set. Escaping just the dot alone in a sed command for some reason doesn't work. The dot is still expanded to a single character wild card.

有趣的是,如果您只想搜索并替换点,则必须将点放在字符集中。出于某种原因,仅在sed命令中单独转义点不起作用。点仍然扩展为单个字符外卡。

$ bash --version  # Ubuntu Lucid (10.04)

GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)

GNU bash,版本4.1.5(1)-release(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

AA-BB-CC

With the addition of leading characters in the search, the escape works.

通过在搜索中添加前导字符,逃生工作。

$ echo foo. | sed s/foo\./foo_/g  # works

foo_

foo_

Putting the dot in a character set of course also works.

将点放在字符集当然也有效。

$ echo foo. | sed s/foo[.]/foo_/g  # also works

foo_

foo_

-- Paul

- 保罗

#4


3  

You need to escape the dot - an unescaped dot will match any character after foo.

你需要逃离点 - 未转义的点将匹配foo之后的任何角色。

sed 's/foo\./foo_/g'

#5


0  

For myself, sed 's/foo\./foo_/g' is working. But you can also try:

对我自己来说,sed's / foo \ ./ foo_ / g'正在运行。但你也可以尝试:

sed -e 's/foo\./foo_/g'

and

sed -e "s/foo\\./foo_/g"

and

sed 's/foo[.]/foo_/g'

#1


18  

Escape the .:

逃离。:

sed 's/foo\./foo_/g' file.php

sed's / foo \ ./ foo_ / g'file.php

Example:

例:

~$ cat test.txt 
foo.bar
~$ sed 's/foo\./foo_/g' test.txt 
foo_bar

#2


6  

Escape the dot with a \

用一个\逃脱点

sed 's/foo\./foo_/g' file.php

If you find the combination of / and \ confusing you can use another 'separator' character

如果您发现/和\ confusing的组合,您可以使用另一个'分隔符'字符

sed 's#foo\.#foo_#g

The 2nd character after the s can be anything and sed will use that character as a separator.

s之后的第二个字符可以是任何东西,sed将使用该字符作为分隔符。

#3


6  

Interestingly, if you want to search for and replace just the dot, you have to put the dot in a character set. Escaping just the dot alone in a sed command for some reason doesn't work. The dot is still expanded to a single character wild card.

有趣的是,如果您只想搜索并替换点,则必须将点放在字符集中。出于某种原因,仅在sed命令中单独转义点不起作用。点仍然扩展为单个字符外卡。

$ bash --version  # Ubuntu Lucid (10.04)

GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)

GNU bash,版本4.1.5(1)-release(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

AA-BB-CC

With the addition of leading characters in the search, the escape works.

通过在搜索中添加前导字符,逃生工作。

$ echo foo. | sed s/foo\./foo_/g  # works

foo_

foo_

Putting the dot in a character set of course also works.

将点放在字符集当然也有效。

$ echo foo. | sed s/foo[.]/foo_/g  # also works

foo_

foo_

-- Paul

- 保罗

#4


3  

You need to escape the dot - an unescaped dot will match any character after foo.

你需要逃离点 - 未转义的点将匹配foo之后的任何角色。

sed 's/foo\./foo_/g'

#5


0  

For myself, sed 's/foo\./foo_/g' is working. But you can also try:

对我自己来说,sed's / foo \ ./ foo_ / g'正在运行。但你也可以尝试:

sed -e 's/foo\./foo_/g'

and

sed -e "s/foo\\./foo_/g"

and

sed 's/foo[.]/foo_/g'