How to extract domain name from url?

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

    With Ruby you can use the Domainatrix library / gem

    http://www.pauldix.net/2009/12/parse-domains-from-urls-easily-with-domainatrix.html

    require 'rubygems'
    require 'domainatrix'
    s = 'http://www.champa.kku.ac.th/dir1/dir2/file?option1&option2'
    url = Domainatrix.parse(s)
    url.domain
    => "kku"
    

    great tool! :-)

提交回复
热议问题