Sed and regex: how to replace up to a certain string
I'm trying to use sed to match a particular string and everything up until some other string. For example, I want to replace "foo" and everything up until "XX" with "bar". However, if "XX" is not in the string, then it should replace everything up until the end of the line.
"foo blah blah XX more text" should become "bar XX more text"
"foo blah blah more text" should become "bar"
Here is what I have so far:
sed 's/foo\(.\(\?!XX\)\)*/bar/g' < input.txt
Thanks for your help!