How to extract domain name from url?

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

    One solution that would cover for more cases would be based on sed regexps:

    echo http://example.com/index.php | sed -e 's#^https://\|^http://##' -e 's#:.*##' -e 's#/.*##'

    That would work for URLs like: http://example.com/index.php, http://example.com:4040/index.php, https://example.com/index.php

提交回复
热议问题