How to extract domain name from url?

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

    basename "http://example.com"
    

    Now of course, this won't work with a URI like this: http://www.example.com/index.html but you could do the following:

    basename $(dirname "http://www.example.com/index.html")
    

    Or for more complex URIs:

    echo "http://www.example.com/somedir/someotherdir/index.html" | cut -d'/' -f3
    

    -d means "delimiter" and -f means "field"; in the above example, the third field delimited by the forward slash '/' is www.example.com.

提交回复
热议问题