阅读背景:

Regex将替换每行中字符串的最后一次出现

来源:互联网 

I am using sed -e 's/\(.*\)ABC/

I am using sed -e 's/\(.*\)ABC/\1DEF/' myfile to replace the last occurrence of ABC with DEF in a file.

我正在使用sed -e 's/\(.*\)ABC/\1DEF/' myfile用DEF替换文件中最后出现的ABC。

I want to modify it to replace the last occurrence of ABC with DEF in each line in the file.

我想修改它,将文件中每一行中最后出现的ABC替换为DEF。

Is it possible to do with regex ?

可以用regex吗?

Thanks

谢谢

1 个解决方案

#1


4  

You need to add 'g' to the end of your sed:

你需要在你的sed结尾加上“g”:

sed -e 's/\(.*\)ABC/\1DEF/g'

This tells sed to replace every occurrence of your regex ("globally") instead of only the first occurrence.

这告诉sed替换您的正则表达式(“全局”)的每一个发生,而不是第一次出现。

EDIT: You should also add a $, if you want to ensure that it is replacing the last occurrence of ABC on the line:

编辑:你也应该添加一个$,如果你想要确保它正在取代最后一个ABC的出现:

sed -e 's/\(.*\)ABC$/\1DEF/g'

DEF/' myfile
to replace the last occurrence of t




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: