Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list of urls like:

hxxp://url.com/subpage.html
hxxp://www.url2.com/index.php
hxxp://subdomain.url3.com/somepage.php
...

How can I use grep to match the domain names only?

All the urls have a / after the domain. And there are a lot of tlds, not sure how many, the list is quite big.

share|improve this question

In order to use non-greedy regexes with grep you will need to use the -P option and the -o option outputs only the matching portion. You will also need to use lookarounds so that part of the match is not included in the output.

grep -Po '.*?//\K.*?(?=/)'

Example:

$ echo 'hxxp://subdomain.url3.com/somepage.php' | grep -Po '.*?//\K.*?(?=/)'
subdomain.url3.com
share|improve this answer

There is a great place to test your regex skills here. The expression should look like

.*?//(.*)/

You will need to loop through all the results. On the page that I have given you, you can put this expression in and a web address and it will then show you what matched. Also remember that you will then have the captured variable for only a limited time.

share|improve this answer
    
That won't work for http://url.com/directory/page.html – Karl Bielefeldt Feb 2 '11 at 8:53

If they only have one TLD after the domain then this should work (I'm assuming you want to exclude the subdomain):

[^\./]*\.[^\./]*/

It still has the trailing slash though, but you can just pipe that through sed.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .