Issue
In GitLab CI script I wanted to
- Remove any character other then numbers and dot (.) and
- 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
Solution
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')
Answered By - Wiktor Stribiżew Answer Checked By - Marie Seifert (WPSolving Admin)