0

How to parse through branchname so that the branch name can be extracted for further processing?

branchname=$(git status 2>&1) 

Parse branchname to extract test_pbx_voice_chanls_e1_WIP in another variable.

From the output shown below, I an trying to extract the name after 'On branch' to be used in other steps in the script.

Output at the prompt:

testing@test:~/linuxprompt-test$ git status
On branch test_pbx_voice_chanls_e1_WIP
Your branch is up-to-date with 'origin/test_pbx_voice_chanls_e1_WIP'

atline
  • 28,355
  • 16
  • 77
  • 113
RSSregex
  • 179
  • 7

2 Answers2

1

You can use git status and sed to parse the branch name:

$ branchname=$(git status 2> /dev/null | sed -e '/^[^O]/d' -e 's/On branch \(.*\)/\1/') | sed -e ':a;N;$!ba;s/\n//g'
$ echo ${branchname}

The first sed will remove lines without "On branch" and then remove "On branch " at the remaining line.

  • -e tells sed to take the next argument as an editing command.

  • ^[^O] will match all lines that don't have "O" at the begining and /d will delete them.

  • s/On branch \(.*\)/\1/ will substitute "On branch ", folowed by any other characters .*, with the characters at the first \1 parenthesis occurance \( and \) that match it.

  • You can read more about s (substitute) command at gnu.org

The second sed -e ':a;N;$!ba;s/\n//g' removes the trailing LFs (x0A) which introduced by the previous sed. This will read the text stream, in a loop, then remove the newline(s).

  • :a Create a label.
  • N Append the current and next line to the pattern space.
  • $! Don't do it on the last line (we need one final newline)
  • ba Branch to the created label.
  • s/\n//g Substitute every newline with nothing.

So, to be fair, using only git status and sed does not produce the simpler solution.

  • The solution work. Just that the branchname has space(s) and It has to be processed further to remove the space. Can the sed be improved to remove the space? This whitespace causes an error when the branch name is used for a git push. branchname=$(git status 2> /dev/null | sed -e '/^[^O]/d' -e 's/On branch \(.*\)/\1/') branchnmrmspc="$(echo -e "${branchname}" | tr -d '[:space:]')" – RSSregex Jan 24 '19 at 16:51
  • @RSSregex There were multiple trailing newlines, which are in correlation to how many lines are returned from the git command. Because I couldn't improve the existing sed command, I added one more sed command just to eliminate the unnessasary newlines. – SkarmoutsosV Jan 28 '19 at 16:42
  • @tripleee You marked this question as duplicate. The link you provided does not cover this question and, unless there is an answer somewere else, the duplicate marking should be removed. – SkarmoutsosV Jan 28 '19 at 17:07
0

In bash you can use simple combination of head and cut:

$ git status
On branch test_pbx_voice_chanls_e1_WIP
Your branch is up-to-date with 'origin/test_pbx_voice_chanls_e1_WIP'

Get first line with head command

$ git status | head -n1
On branch test_pbx_voice_chanls_e1_WIP

  • head -nX will return X line from the beginning (a.k.a. head) from provided input

Get third word, which is always a branch name:

$ git status | head -n1 | cut -d" " -f3
test_pbx_voice_chanls_e1_WIP
  • -d" " will split cut's input into array of strings by a " " (space)
  • -f3 will return 3rd field of that array

Assign output to variable and possibly throw away error messages (2>/dev/null):

$ branchname=$(git status 2>/dev/null | head -n1 | cut -d" " -f3)
$ echo ${branchname}
test_pbx_voice_chanls_e1_WIP
hradecek
  • 2,455
  • 2
  • 21
  • 30