I have these two


http://home.example.com:8888/index.php (home can be anything but www)

and want to get this:



Preferably in some readable way so I understand how it works in a year from now.

This is my attempt:

echo http://www.example.com:8888/index.php | awk -F[/:\'www\'] '{print $7}'

but the result is

.example.com (note the dot) when applied to www.example.com

it works fine for home.example.com if I print $4 but I would prefer if I could use the same print-clause in both cases.

Thank you.

  • 2
    awk -F'[/:]' '{gsub(/www./,""); print $4}' – jasonwryan Jun 13 '18 at 6:33

sed solution:

sed -E 's~http://(www\.)?([^:]+).*~\2~' file
  • ~ - treated as sed subcommand separator
  • -E - allows extended regexps
  • (www\.)? - matches optional www. part
  • ([^:]+) - matches any character sequence except :

The output:


In three steps:

  1. Remove everything up to the :// in the URL.
  2. Remove everything after the first / or : in the remaining string. This now leaves you with only the hostname from the original URL.
  3. Remove bits that you don't want, e.g. www. from the start of the hostname.
sed -e 's@^.*://@@' -e 's/[/:].*$//' -e 's/^www\.//'

Running this on your example URLs will yield


Using awk:

awk -F'[/:]+' '{ sub("^www\.", "", $2); print $2 }'

This treats the input lines as records of fields delimited by any number of consecutive : or /. This means that the hostname will be available in the second field on each line.

The sub() will simply remove the bit of the hostname that we are not interested in (here, www. at the start of the hostname).


Using GNU awk and its field pattern definition FPAT:

 awk -v FPAT='([^/]+\\.)+[^:/]+' '{print $1}'

The field pattern is set to a sequence of character excluding the / followed by a dot . and must terminate with either : or /.

Maybe a little too permissive for an URL but it catches fqdn with and without port number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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