Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Sorting list of domain names by TLD and subdomain

by grinder (Bishop)
on Oct 15, 2003 at 21:15 UTC ( #299555=snippet: print w/ replies, xml ) Need Help??
Description:

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
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2016-06-30 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...











    Results (405 votes). Check out past polls.