Sed – Stop Repeating Pattern Replacement Once a Certain String is Reached

sed

I can't find the answer to this anywhere. It sounds simple but I'm starting to think maybe it's not.

I want sed to remove all the CATs before the STOP in this string:

two CAT two four CAT CAT seven one STOP four CAT two CAT three

So the output I'm hoping for will be:

two two four seven one STOP four CAT CAT two CAT three

There could be any number of CATs anywhere in the string. The stop marker can be anywhere too, but just one of them, and always spelled STOP.

(Edit: as pointed out below my question is ambiguous – must CAT have adjacent spaces or can any chars border it? Maybe only non-alphanumeric chars are ok? Presenting my actual use case was intense (a big bash function) so I simplified, too much. Readers please bear in mind that solutions below may make different assumptions about adjacency. Thanks)

Best Answer

You could replace one at a time in a loop, until there are no more CATs before the STOP:

$ echo 'two CAT two four CAT CAT seven one STOP four CAT two CAT three' |
    sed -e :a -e '/CAT.*STOP/s/CAT //;ta'
two two four seven one STOP four CAT two CAT three
Related Question