I recently was working on assembling two Git repositories i.e. a core and a view in an automated build system which seemed to be unsupported when using Jenkins freestyle mode. The solution is to use a more flexible approach using Jenkins pipeline job but I found myself in a situation where I have to extract a word from a Git URL using shell script.
It's been a while since I last touched Perl or regular expressions so it's sort of a struggle. Surprisingly, I can't find a straight forward answer in the Internet and I have to build my own.
To be precise I have to extract the Git repository name from the following Git URL:
My chosen solution was to use
sed and goes as follows:
export GITfirstname.lastname@example.org:project/repo-name.git echo $GIT | sed 's/^.*\/\(.*\)\.git$/\1/'
Running this script should extract the word
The important part of the script is:
Using regular expressions, just insert some patterns between the left anchor
^ and right anchor
$. In my case it's the word between the
.git and put the word you want to capture inside the parenthesis.
And nope I won't go explaining this script in more detail since this is not a comprehensive
sed, regular expression or shell script tutorial. In case you need a fast and reliable way to extract a word from a URL or a phrase using shell script I'm already glad to have help somebody step in the right direction.