How to extract domain name from url?

前端 未结 12 1666
走了就别回头了
走了就别回头了 2020-12-13 05:46

How do I extract the domain name from a url using bash? like: http://example.com/ to example.com must work for any tld, not just .com

12条回答
  •  孤城傲影
    2020-12-13 06:41

    Instead of using regex to do this you can use python's urlparse:

     URL=http://www.example.com
    
     python -c "from urlparse import urlparse
     url = urlparse('$URL')
     print url.netloc"
    

    You could either use it like this or put it in a small script. However this still expects a valid scheme identifier, looking at your comment your input doesn't necessarily provide one. You can specify a default scheme, but urlparse expects the netloc to start with '//' :

    url = urlparse('//www.example.com/index.html','http')

    So you will have to prepend those manually, i.e:

     python -c "from urlparse import urlparse
     if '$URL'.find('://') == -1 then:
       url = urlparse('//$URL','http')
     else:
       url = urlparse('$URL')
     print url.netloc"
    

提交回复
热议问题