Search code examples
bashgitposix

Extracting git url's domain with POSIX


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?


Solution

  • 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