Why is my regex not working using sed in bash script on Mac OSX?
You need to add -E
to the sed
command line to make it use extended regular expressions:
sed -E 's/^([A-Za-z]+-[0-9]+)/\1/'
If what you want to do is to shorten the string CBX-1234--CBX-5678
to CBX-1234
, you additionally need to modify the substitution to take the whole string into account:
sed -E 's/^([A-Za-z]+-[0-9]+).*/\1/'
You may alternatively use the bash
parameter expansion
shortenedString="${string%%--*}"
This will remove everything from $string
from the first occurrence of --
.