Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

3 Answers

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.