0

I have list of URLs like this:

http://example.com/sdfsdf/sdfsa
https://example2.com/53lasfd/asdfs
http://www.example3.com/asdfas/asdfasdf.php?=asdfa
https://subdomain.example4.com/index.php?id=sadfa
https://www.subdomain.example5.com/asdfas/asdfasd

I need to extract only domains (and subdomains) without http, https, www and all after /:

exmaple.com
exmaple2.com
example3.com
subdomain.example4.com
subdomain.example5.com
2
  • Sorry, output should be each at new line Commented Oct 1, 2015 at 10:37
  • I actually do not understand your question, because you said you want subdomain, but www is a subdomain. Commented Jul 31, 2021 at 6:54

1 Answer 1

2

You can use awk,

awk -F/ '{sub(/^www\.?/,"",$3); print $3}' yourfile

Test:

$ awk -F/ '{sub(/^www\.?/,"",$3); print $3}' yourfile
example.com
example2.com
example3.com
subdomain.example4.com
subdomain.example5.com
Sign up to request clarification or add additional context in comments.

4 Comments

Exactly what I needed! Thanks!
And what if I do not need subdomains but only domains?
Try : awk -F/ '{sub(/^www\.?/,"",$3); print $3}' yourfile | awk -F\. 'NF==2'
I definitely clicked UP, but it in someway gone -1, how can I edit this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.