Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://299555]
[Tux]: chacham I did not want to also list Committed changes to the core or core modules and (Co-)Organized a perl event

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2017-05-30 14:33 GMT
Find Nodes?
    Voting Booth?