I have a text file that contains following
https://git.centos.org/git/rpms/abc.git
https://git.centos.org/git/rpms/abc.git/
https://git.centos.org/git/rpms/abc
When I run the following command,
reponame=$(echo $url | awk -F/ '{print $NF}' | sed -e 's/.git\/$//' | sed -e 's/.git//')
echo $reponame
I am supposed to get
abc
It fails for the lines ending in .git/
but it works for the other 2 cases.
Best Answer
You have to remove the trailing slash before you print the last field with awk. Otherwise the last field will be empty.
Use
or even
Tips:
sed
commands to one invocation of sed so it is sometimes not necessary to pipe fromsed
tosed
. Eithersed -e 'cmd1' -e 'cmd2' ...
orsed 'cmd1;cmd2;...'
will work.s
command ofsed
so you do not have to escape slashes in the pattern (I used#
as a delimiter).