I am using I am using 我正在使用sed -e 's/\(.*\)ABC/\1DEF/' myfile用DEF替换文件中最后出现的ABC。 I want to modify it to replace the last occurrence of 我想修改它,将文件中每一行中最后出现的ABC替换为DEF。 Is it possible to do with regex ? 可以用regex吗? Thanks 谢谢 4 You need to add 'g' to the end of your sed: 你需要在你的sed结尾加上“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 编辑:你也应该添加一个$,如果你想要确保它正在取代最后一个ABC的出现:sed -e 's/\(.*\)ABC/ to replace the last occurrence of tsed -e 's/\(.*\)ABC/\1DEF/' myfile to replace the last occurrence of ABC with DEF in a file. ABC with DEF in each line in the file.1 个解决方案
#1
sed -e 's/\(.*\)ABC/\1DEF/g'
$, if you want to ensure that it is replacing the last occurrence of ABC on the line:sed -e 's/\(.*\)ABC$/\1DEF/g'
DEF/' myfile