2

In GitLab CI script I wanted to

  1. Remove any character other then numbers and dot (.) and
  2. remove all the text after 2nd dot [Ex 5.6.7.8 -> 5.6] if dot exists in text.

So for that I have tried to use sed , but it's not working in GitLab Script (working on bash shell locally)

export BRANCH_NAME={$CI_COMMIT_REF_NAME} | sed 's/\.[^.]*$//'  | sed 's/[^0-9.]//g'

any idea what is missing here ?

If sed is not going to work here then any other option to achieve the same?

Edit : As per @WiktorStribiżew - echo "$CI_COMMIT_REF_NAME" | sed 's/.[^.]$//' | sed 's/[^0-9.]//g' - export BRANCH_NAME=${CI_COMMIT_REF_NAME} | sed 's/.[^.]$//' | sed 's/[^0-9.]//g'

echo is working but export is not

4
  • 2
    Does echo "$CI_COMMIT_REF_NAME" | sed 's/\.[^.]*$//' | sed 's/[^0-9.]//g' work? Also, you probably meant ${CI_COMMIT_REF_NAME}, not {$CI_COMMIT_REF_NAME} Commented Jan 11, 2022 at 21:27
  • I go with @WiktorStribiżew - i can also clarify that there is no reason sed should not work in gitlab pipelines (unless it is not installed on the underlying system) Commented Jan 11, 2022 at 21:34
  • @WiktorStribiżew echo "$CI_COMMIT_REF_NAME" | sed 's/\.[^.]*$//' | sed 's/[^0-9.]//g' is working but -export BRANCH_NAME=${CI_COMMIT_REF_NAME} | sed 's/\.[^.]*$//' | sed 's/[^0-9.]//g' is not working !!! Commented Jan 11, 2022 at 21:58
  • 1
    Yeah, just wrap with $(...). See my full answer. Commented Jan 11, 2022 at 21:59

2 Answers 2

1

Using sed

$ export BRANCH_NAME=$(echo "$CI_COMMIT_REF_NAME" |sed 's/[A-Za-z]*//g;s/\([0-9]*\.[0-9]*\)\.[^ ]*/\1/g')
Sign up to request clarification or add additional context in comments.

2 Comments

I think you used sed 's/[A-Za-z]*//g to remove Ex, right? I think it was just a shortened word "example", the string OP just wanted to share was 5.6.7.8.
@WiktorStribiżew Indeed. Thanks for pointing that out.
0

First of all, to remove all text after and including second dot you need

sed 's/\([^.]*\.[^.]*\).*/\1/'

The sed 's/\.[^.]*$//' sed command removes the last dot and any text after it.

Next, you must have made a typo and you actually meant to write ${CI_COMMIT_REF_NAME} and not {$CI_COMMIT_REF_NAME}.

So, you might be better off with

export BRANCH_NAME=$(echo "$CI_COMMIT_REF_NAME" | sed 's/\([^.]*\.[^.]*\).*/\1/' | sed 's/[^0-9.]//g')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.