Regex to replace last occurrence of a string in each line
19,608
You need to add 'g' to the end of your sed:
sed -e 's/\(.*\)ABC/\1DEF/g'
This tells sed to replace every occurrence of your regex ("globally") instead of only the first occurrence.
EDIT: You should also add a $
, if you want to ensure that it is replacing the last occurrence of ABC on the line:
sed -e 's/\(.*\)ABC$/\1DEF/g'
Related videos on Youtube
Author by
Michael
Updated on June 01, 2022Comments
-
Michael about 2 years
I am using
sed -e 's/\(.*\)ABC/\1DEF/' myfile
to replace the last occurrence ofABC
withDEF
in a file.I want to modify it to replace the last occurrence of
ABC
withDEF
in each line in the file.Is it possible to do with regex ?
Thanks
-
anubhava about 13 yearsActually
sed -e 's/\(.*\)ABC/\1DEF/' will replace last occurrence of
ABC` in each line of the file.
-