Reputation: 2095
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?
Upvotes: 1
Views: 124
Reputation: 614
string=$(config --local remote.origin.url | tail -c -21)
var1=$(echo "${string}" | cut -d'/' -f1)
var2=$(echo "${string}" | cut -d'/' -f2 | sed s'@\.git@@')
If you have multiple urls with variable lengths, this will not work, but if you only have the one, it will.
var1=smat
var2=sma-mes-test.git
If I did have something variable, personally I would replace all of the forward slashes with carriage returns, throw them into a file, and then export the last and second last lines with ed, which would give me the two last segments of the url.
Regular expressions literally give me a migraine headache, but as long as I can get everything on its' own line, I can quite easily bypass the need for them entirely.
Upvotes: 0
Reputation: 785611
Your sed
is pretty close. You can just extend it to capture 2 values and print them:
git config --local remote.origin.url |
sed -E 's~.*/([^/]+)/([^.]+)\.git$~\1 \2~'
smat sma-mes-test
If you want to populate shell variable using these 2 values then use this read
command in bash
:
read v1 v2 < <(git config --local remote.origin.url |
sed -E 's~.*/([^/]+)/([^.]+)\.git$~\1 \2~')
# check variable values
declare -p v1 v2
declare -- v1="smat"
declare -- v2="sma-mes-test"
Upvotes: 2
Reputation: 11237
Using sed
$ sed -E 's#.*/([^/]*)/#\1 #' input_file
smat sma-mes-test.git
Upvotes: 1
Reputation: 133640
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)}'
Upvotes: 2
Reputation: 36630
I would harness GNU AWK
for this task following way, let file.txt
content be
https://[email protected]/scm/smat/sma-mes-test.git
then
awk 'BEGIN{FS="/"}{sub(/\.git$/,"",$NF);print $(NF-1),$NF}' file.txt
gives output
smat sma-mes-test
Explanation: I instruct GNU AWK
that field separator is slash character, then I replace .git
(observe that .
is escaped to mean literal dot) adjacent to end ($
) in last field ($NF
), then I print 2nd from end field ($(NF-1)
) and last field ($NF
), which are sheared by space, which is default output field separator, if you wish to use other character for that purpose set OFS
(output field separator) in BEGIN
. If you want to know more about NF
then read 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
(tested in gawk 4.2.1)
Upvotes: 0