Reputation: 3128
I'm trying to build the most robust way to extract the domain from a git repo. For urls like:
ssh://[email protected]:22411/usage/project_100.git
git://example.org/path/to/repo.git
https://github.com/example/foobar.git
http://github.com/example/foobar.git
ssh://[email protected]/path/to/repo.git
git://host.com/path/to/repo.git
I can use:
echo $url | awk -F[/:] '{print $4}'
But for repos like:
"[email protected]:User/UserRepo.git"
It won't work. But the following does:
echo $url | awk -v FS="(@|:)" '{print $2}'
Is there some robust way I could always exctract the domain in POSIX?
Upvotes: 2
Views: 481
Reputation: 88776
With sed. I switched from s///
to s|||
.
sed 's|.*//||; s|.*@||; s|/.*||; s|:.*||' file
output:
gitlab.com example.org github.com github.com host.com host.com
Upvotes: 2
Reputation: 531878
If the URL contains ://
, you know to drop the protocol, then drop everything from the first /
onwards. Otherwise, if it contains @
, assume it is your second case, and drop everything up to and including the @
, then everything from the :
onwards. Other cases can be added as necessary.
url="..."
case $url in
*://*)
domain=${url#*://}
domain=${domain#*@}
domain=${domain%%/*}
;;
*@*:*)
domain=${url#*@}
domain=${domain%%:*}
;;
esac
Upvotes: 2
Reputation: 1490
You can do that with sed
easily.
echo $url | sed -E 's/.*\:\/\/(.*)@?.*\:.*/\1 /' | awk -F@ '{print $1}'
Upvotes: 0
Reputation: 26707
Perl version :
perl -pe 's{.*//([^/]+@)?([^:/]+).*}{$2}' input-file
Upvotes: 1