Removing pattern at the end of a string using sed or other bash tools

This should work:

echo "DAAAAABCBBBCCABCABC" | sed -e 's/\(ABC\)*$//g'

Result:

DAAAAABCBBBCC

Surround string between parentheses and * applies to all letters inside them in that exact order.


bash can do this internally. The following removes any "ABC" string at the end, and its result can used in variable assignment, a command or whatever:

${String%ABC}

You can also use a regex, not just a simple string match. See http://tldp.org/LDP/abs/html/string-manipulation.html

Tags:

Regex

Bash

Sed