Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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??


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

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

From the shell, it's a snap: perl -ple'$_=join".",reverse split/\./' reverse the domain components ( 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
Comment on Sorting list of domain names by TLD and subdomain
Download Code

Back to Snippets Section

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2015-10-07 03:30 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (170 votes), past polls