I have a text file that contains followinghttps://git.centos.org/git/rpms/abc.githttps://git.centos.org/git/rpms/abc.git/https://git.centos.org/git/rpms/abcWhen I run the following command, reponame=$(echo $url | awk -F/ '{print $NF}' | sed -e 's/.git\/$//' | sed -e 's/.git//')echo $reponameI am supposed to getabcIt fails for the lines ending in .git/ but it works for the other 2 cases.
Remove front-slash using sed
shell;sed
You have to remove the trailing slash before you print the last field with awk. Otherwise the last field will be empty.Use echo $url | sed -e 's#/$##' -e 's/\.git$//' | awk -F / '{print $NF}'or evenecho $url | sed -e 's#/$##' -e 's/\.git$//' -e 's#^.*/##'Tips:You can give several sed commands to one invocation of sed so it is sometimes not necessary to pipe from sed to sed. Either sed -e 'cmd1' -e 'cmd2' ... or sed 'cmd1;cmd2;...' will work.You can use a different delimiter for the s command of sed so you do not have to escape slashes in the pattern (I used # as a delimiter).