Search code examples
linuxurlsed

extract server name from git clone URL


I have this

$ cat test.sh
echo "https://bitbucket.dev.global.server.com/scm/xyz/abd.git"
echo "ssh://git@bitbucket.dev.global.server.com:6699/xyz/abc.git"
echo "http://bitbucket.dev.global.server.com/abc"
echo "ssh://user@bitbucket.dev.global.server.com/xyz/abc"
echo "http://bitbucket.dev.global.server.com"
echo "ssh://user@bitbucket.dev.global.server.com/xyz/abc.git"

I want an one liner command (preferably sed command) that will extract the server name from the URL. e.g.

bitbucket.dev.global.server.com

I tried this but it doesn't work

$ ./test.sh | sed 's/\(\/\/\|\@\)/&\n/;s/.*\n//;s/\(\:\|\/\)/\n&/;s/\n.*//'
bitbucket.dev.global.server.com
git@bitbucket.dev.global.server.com
bitbucket.dev.global.server.com
user@bitbucket.dev.global.server.com
bitbucket.dev.global.server.com
user@bitbucket.dev.global.server.com

It still got the user and @ symbol. How to do this?


Solution

  • I want an one liner command (preferably sed command)

    Since you requested a sed one-liner, you can try this:

    ./test.sh | sed -E 's|.*://([^/@:]+@)?([^/@:]+).*|\2|'
    

    which outputs this:

    bitbucket.dev.global.server.com
    bitbucket.dev.global.server.com
    bitbucket.dev.global.server.com
    bitbucket.dev.global.server.com
    bitbucket.dev.global.server.com
    bitbucket.dev.global.server.com
    

    Explanation:

    ./test.sh | sed -E '
      s| .*://        # Match all up to "://"
         ([^/@:]+@)?  # Match "user@" or "git@"
         ([^/@:]+)    # The hostname
         .*           # Match all after the hostname and discard it
      |\2|'          # Replace full match with hostname, and then it's done