Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Sorting list of domain names by TLD and subdomain

by grinder (Bishop)
on Oct 15, 2003 at 21:15 UTC ( [id://299555]=CUFP: print w/replies, xml ) Need Help??

Ever had an alphabetical (or not) list of domain names:

de.yahoo.com mail.example.org www.aol.com www.example.org www.freshmeat.net www.hotdoggie.com.au

... and wished it was sorted by tld (and recursively down the subdomains)?

www.hotdoggie.com.au www.aol.com de.yahoo.com www.freshmeat.net mail.example.org www.example.org

From the shell, it's a snap: perl -ple'$_=join".",reverse split/\./' reverse the domain components (mail.example.org becomes org.example.mail). This can then be piped to another filter (e.g. sort) and then the same filter can be run again to undo the reversal.

It's even easier if the command is wrapped up in an alias. (And yes, I know about "Useless use of cat(1)". I'm using it here to show the symmetry of the munging/demunging).

cat foo | perl -ple'$_=join".",reverse split/\./' | sort | \ perl -ple'$_=join".",reverse split/\./' # or alias tldmunge='perl -ple'"'"'$_=join".",reverse split/\./'"'" cat foo | tldmunge | sort | tldmunge

Replies are listed 'Best First'.
Re: Sorting list of domain names by TLD and subdomain
by Anonymous Monk on Sep 14, 2018 at 20:50 UTC
    Nice script. Wondering if there's a way to ignore the sub-directories with periods. The script thinks they're domains.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://299555]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-12-13 01:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (67 votes). Check out past polls.