Tuesday, October 25, 2022

[SOLVED] Git URL - Pull out substring via Shell (awk & sed)?

Issue

I have got the following URL:

https://[email protected]/scm/smat/sma-mes-test.git

I need to pull out smat-mes-test and smat:

git config --local remote.origin.url|sed -n 's#.*/\([^.]*\)\.git#\1#p'

sma-mes-test

This works. But I also need the project name, which is smat

I am not really familiar to complex regex and sed, I was able to find the other command in another post here. Does anyone know how I am able to extract the smat value here?


Solution

With your shown samples please try following awk code. Simple explanation would be, setting field separator(s) as / and .git for all the lines and in main program printing 3rd last and 3nd last elements from the line.

your_git_command | awk -F'/|\\.git' '{print $(NF-2),$(NF-1)}'


Answered By - RavinderSingh13
Answer Checked By - Robin (WPSolving Admin)