How to extract domain name from url?

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

    there is so little info on how you get those urls...please show more info next time. are there parameters in the url etc etc... Meanwhile, just simple string manipulation for your sample url

    eg

    $ s="http://example.com/index.php"
    $ echo ${s/%/*}  #get rid of last "/" onwards
    http://example.com
    $ s=${s/%\//}  
    $ echo ${s/#http:\/\//} # get rid of http://
    example.com
    

    other ways, using sed(GNU)

    $ echo $s | sed 's/http:\/\///;s|\/.*||'
    example.com
    

    use awk

    $ echo $s| awk '{gsub("http://|/.*","")}1'
    example.com
    

提交回复
热议问题