0

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?

Ivan
  • 57
  • 11
  • 26
  • 1
    Do you have to do it with `sed`? You can do this with the [parameter expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion) operators. – Barmar Sep 13 '22 at 20:50
  • You should always quote your variables unless you have a good reason not to. – Barmar Sep 13 '22 at 20:51
  • 4
    You're missing the ending delimiter. `s~^$BUGFIX_PREFIX~~` – Barmar Sep 13 '22 at 20:51
  • 1
    You don't need to run `sed` twice, you can use multiple `-e` options on the same call. – Barmar Sep 13 '22 at 20:54
  • You don't need to export all your variables, unless you run some external command that needs them. – glenn jackman Sep 13 '22 at 21:52
  • Barmar, you were right. I was missing the end limiter. – Ivan Sep 14 '22 at 05:23

1 Answers1

0

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'@$@\"@'
petrus4
  • 616
  • 4
  • 7