Issue
I want to extract Jira ticket number from the branch name with sed. This is what I have
echo "PTW-123-branch-name" | sed 's/.*\([A-Z]+-[0-9]+[^-]\).*/\1/'
expected result: PTW-123
What is wrong with the regexp?
Solution
You may use this sed
:
echo "PTW-123-branch-name" | sed 's/\([0-9]\)-.*$/\1/'
PTW-123
Details:
\([0-9]\)-
: Matches a digit and captures it in group #1 followed by hyphen.*$
: Match remaining string until end\1
: Is replacement that puts captured digit back in output
Alternatively you can use cut
also:
echo "PTW-123-branch-name" | cut -d- -f1,2
PTW-123
Answered By - anubhava Answer Checked By - Marie Seifert (WPSolving Admin)