How to extract domain name from url?

前端 未结 12 1663
走了就别回头了
走了就别回头了 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:35

    sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_'
    

    e.g.

    $ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://example.com'
    example.com
    
    $ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'https://example.com'
    example.com
    
    $ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://example.com:1234/some/path'
    example.com
    
    $ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://user:pass@example.com:1234/some/path'
    example.com
    
    $ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://user:pass@example.com:1234/some/path#fragment'
    example.com
    
    $ sed -E -e 's_.*://([^/@]*@)?([^/:]+).*_\2_' <<< 'http://user:pass@example.com:1234/some/path#fragment?params=true'
    example.com
    

提交回复
热议问题