1

For example I have a git branch name feature/ABC-123-my-stuff

I want to just capture ABC-123 in this format.

I tried

cut -d "/" -f2 <<< "$branchName"

result in

ABC-123-my-stuff

but I want to only keep the string right after the / and before 2nd -

What do I add / modify to achieve that?

NOTE: I am using zsh on MacOS

oguz ismail
  • 1
  • 16
  • 47
  • 69
ey dee ey em
  • 7,991
  • 14
  • 65
  • 121

2 Answers2

2

Use cut 2 times:

(cut -d"/" -f2 | cut -d"-" -f1,2) <<< $branchName

or with echo:

echo $branchName | cut -d"/" -f2 | cut -d"-" -f1,2

Another way you can use grep:

echo $branchName | egrep -o '[A-Z]{3}-[0-9]{3}'

Note: this solution will work if you have every time 3 times capital letter, then -, then 3 digits.

All solutions gives me the output:

ABC-123
Mikołaj Głodziak
  • 4,775
  • 7
  • 28
  • How can I store the first approach into a var? I tried ticketName=(cut -d"/" -f2 | cut -d"-" -f1,2) <<< $branchName; `syntax error near unexpected token `|'` But it gave me error of – ey dee ey em Jun 24 '21 at 14:18
  • @Ezeewei, `var=$((cut -d"/" -f2 | cut -d"-" -f1,2) <<< $branchName)` – Mikołaj Głodziak Jun 24 '21 at 14:20
  • I am noobie, why we need that `$()`? – ey dee ey em Jun 24 '21 at 14:21
  • 1
    If you want to put the output of commands in a variable you have to use `$()` or \`\`. This is an element of Bash syntax. [Here](https://stackoverflow.com/questions/4708549/what-is-the-difference-between-command-and-command-in-shell-programming) you can find professional explanation. – Mikołaj Głodziak Jun 24 '21 at 14:25
1

Use regexp matching:

 if [[ $branchName =~ /([^-]+-[^-]+)- ]]
 then
   desired_part=$match[1]
 else
   echo $branchName has not the expected format
 fi
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • I got syntax error.... I tried to use this in VS Code terminal in Mac – ey dee ey em Jun 24 '21 at 14:07
  • Which zsh-Version are you using? Mine is 5.8. Also, plesae copy and paste the full error message you got. – user1934428 Jun 24 '21 at 14:09
  • ``` ./test2.sh: line 20: syntax error near unexpected token `else' ./test2.sh: line 20: ` else' ``` – ey dee ey em Jun 24 '21 at 14:15
  • Copy and paste error of my part. Updated. Aside from this: Your error message sounds odd. I would have expected that you have gotten _zsh: parse error near `else'_. You didn't mention yet your zsh version. – user1934428 Jun 24 '21 at 14:17
  • Now the result is only `[1]` – ey dee ey em Jun 24 '21 at 14:20
  • btw I have zsh 5.7.1 (x86_64-apple-darwin19.0) – ey dee ey em Jun 24 '21 at 14:36
  • See the screenshot [here](https://postimg.cc/WFrr3Mkq). AFIK, I'm not using any feature which was introduced in 5.8. Could you also contribute a screenshot from your side? – user1934428 Jun 24 '21 at 14:37
  • it looks Windows OS.... I am not sure if the zsh on win is the same as zsh Mac? as the chosen answer worked and I do not yet know why your answer is not working for me... but thanks for contributing! and I like the approach to add IF ELSE to avoid error – ey dee ey em Jun 24 '21 at 15:23
  • It is cygwin inside Windows, but the zsh is built from the same sources. There are certainly no differences in the way regular expressions are calculated. I would need to see a trace (`set -x`) from your side, to comment what is going on there. So far you didn't even post a screenshot, sadly .... If I were you, I would investigate this, because I would feel uncomfortable if my shell somehow behaves differently from what it is supposed to. – user1934428 Jun 25 '21 at 06:29