Using sed to remove prefix with slash from string

Viewed 42

I'm extracting a Jira issue from a string using sed. I want to get rid of prefixes too.

My possible prefixes are:

FEATURE_PREFIX="feature/"
BUGFIX_PREFIX="bugfix/"

I've tried three different ways to use sed despite the slash in the prefix but nothing seems to work.

First try:

export UNFIXED_ID=$(echo ${CI_MERGE_REQUEST_TITLE} | sed -e "s~^$BUGFIX_PREFIX~/" | sed -e "s~^$FEATURE_PREFIX~/")
export MERGE_REQUEST_JIRA_ID=$(echo ${UNFIXED_ID} | sed -r "s/^([A-Za-z][A-Za-z0-9]+-[0-9]+).*/\1/") 
echo ${MERGE_REQUEST_JIRA_ID}

gives the error sed: unmatched '~'

Second try:

export UNFIXED_ID=$(echo ${CI_MERGE_REQUEST_TITLE} | sed -e "s~^$BUGFIX_PREFIX/~" | sed -e "s~^$FEATURE_PREFIX/~")
export MERGE_REQUEST_JIRA_ID=$(echo ${UNFIXED_ID} | sed -r "s/^([A-Za-z][A-Za-z0-9]+-[0-9]+).*/\1/") 
echo ${MERGE_REQUEST_JIRA_ID}

gives the error sed: unmatched '~'

Third try:

export UNFIXED_ID=$(echo ${CI_MERGE_REQUEST_TITLE} | sed -e "s~^$BUGFIX_PREFIX~" | sed -e "s~^$FEATURE_PREFIX~")
export MERGE_REQUEST_JIRA_ID=$(echo ${UNFIXED_ID} | sed -r "s/^([A-Za-z][A-Za-z0-9]+-[0-9]+).*/\1/") 
echo ${MERGE_REQUEST_JIRA_ID}

gives the error sed: unmatched '~'

As per this question Sed error : bad option in substitution expression I thought it was just a matter of replacing the / by ~

What am I failing to do here with the delimiter?

1 Answers

I apologise if I have misunderstood the question, but if the two variables are on their own lines, then you can probably just search for and print the entire line:-

sed -n '/FEATURE_PREFIX/p' file | \
cut -d'=' -f2 | \
head -c -3 | \
sed s'@$@\"@'
Related