Search code examples
bashsedgrepcut

Strip only domain name out of input url string


Did a bit of searching already but cannot seem to find an elegant way of doing this. I'd like to be able to search through a list like below and only end up with a plain text output file containing on the domain name, no http:// or anything after the /

So a list like this:

http://7wind.ru/file/Behind+the+dune/
http://aldersgatencsc.org/open.php?utm_source=5r2ke0ow6k&utm_medium=qqod2h9a88&utm_campaign=2d1hl1v8c5&utm_term=mz34ligqc4&utm_content=bgi71kl5oy
http://amunow.org/test.php?utm_source=5r2ke0ow6k&utm_medium=qqod2h9a88&utm_campaign=2d1hl1v8c5&utm_term=dhxg1r4l76&utm_content=tr71txtklp

I want to end up with plain text output file like this.

7wind.ru
aldersgatencsc.org
amunow.org

Solution

  • Given:

    $ echo "$txt"
    http://7wind.ru/file/Behind+the+dune/
    http://aldersgatencsc.org/open.php?utm_source=5r2ke0ow6k&utm_medium=qqod2h9a88&utm_campaign=2d1hl1v8c5&utm_term=mz34ligqc4&utm_content=bgi71kl5oy
    http://amunow.org/test.php?utm_source=5r2ke0ow6k&utm_medium=qqod2h9a88&utm_campaign=2d1hl1v8c5&utm_term=dhxg1r4l76&utm_content=tr71txtklp
    

    You can use cut:

    $ echo "$txt" | cut -d'/' -f3
    7wind.ru
    aldersgatencsc.org
    amunow.org
    

    Or, if your content is in a file:

    $ cut -d'/' -f3 file
    7wind.ru
    aldersgatencsc.org
    amunow.org
    

    Then redirect that to the file you want:

    $ cut -d'/' -f3 file >new_file