Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:


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.*?(?=/)'


$ echo 'hxxp://' | grep -Po '.*?//\K.*?(?=/)'
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 – 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

Your Answer


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.