-1

I need to remove everything from a URL and keep only domain.

Before sed examples:

https://www.something.com/something/something
https://www.something.com:8080/something/something

After sed:

something.com

1

4 Answers 4

2

Something like this?

$ url1='https://www.something.com:8080/something/something'
$ url2='http://www.someting.com/something/something'
$ printf "%s\n%s\n" "$url1" "$url2" | \
>   sed -e 's|^.*://||' \
>       -e 's|/.*$||' \
>       -e 's|:.*$||' \
>       -e 's|^.*@||' \
www.something.com
www.someting.com

This chains four sed expressions together:

  • s|^.*://||: Delete everything from the start up-to and including ://
  • s|/.*$||: Delete everything remaining from the first slash to the end
  • s|:.*$||: Delete everything remaining from the first colon to the end
  • s|^.*@||: deletes everything up-to and including @ ex: ftp://user:[email protected]

You are left with www.something.com. www is actually part of the domain. (unix.stackexchange.com has a different IP than math.stackexchange.com)

0

Using the URI module of Perl to pull out the hostname from the URL, and then a substitution to delete www. from the start from that hostname:

perl -MURI -ple '$_ = URI->new($_)->host(); s/^www\.//'

Testing:

$ cat file
https://www.something.com/something/something
https://www.something.com:8080/something/something
https://something.com:999/something/something
$ perl -MURI -ple '$_ = URI->new($_)->host(); s/^www\.//' <file
something.com
something.com
something.com
0

With grep implementations that support perl-like regular expressions with -P and a -o option such as GNU grep:

grep -iPo '://([^/@]*@)?(www\.)?\K(\[.*?\]|[^:/]+)'

([^/@]*@)? in an attempt to skip the user:pass@ part if any, \[.*?\] to take care of https://[abcd::cdef]/ipv6 URLs.

Best would be to use a proper URI parser like with @Kusalananda's approach though.

0

Using sed

$ sed -E 's/[^.]*\.([[:alpha:].]+).*/\1/' input_file
something.com
something.com

You must log in to answer this question.

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