<?xml version="1.0" encoding="windows-1252"?>
<node id="299555" title="Sorting list of domain names by TLD and subdomain" created="2003-10-15 17:15:38" updated="2005-08-02 18:01:28">
<type id="1980">
snippet</type>
<author id="29008">
grinder</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
&lt;p&gt;Ever had an alphabetical (or not) list of domain names:&lt;/p&gt;

&lt;code&gt;de.yahoo.com
mail.example.org
www.aol.com
www.example.org
www.freshmeat.net
www.hotdoggie.com.au&lt;/code&gt;

&lt;p&gt;... and wished it was sorted by tld (and recursively down the subdomains)?&lt;/p&gt;

&lt;code&gt;www.hotdoggie.com.au
www.aol.com
de.yahoo.com
www.freshmeat.net
mail.example.org
www.example.org&lt;/code&gt;

&lt;p&gt;From the shell, it's a snap: &lt;code&gt;perl -ple'$_=join".",reverse split/\./'&lt;/code&gt; reverse the domain components (&lt;code&gt;mail.example.org&lt;/code&gt; becomes &lt;code&gt;org.example.mail&lt;/code&gt;). This can then be piped to another filter (&lt;i&gt;e.g.&lt;/i&gt; &lt;tt&gt;sort&lt;/tt&gt;) and then the same filter can be run again to undo the reversal.&lt;/p&gt;

&lt;p&gt;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).&lt;/p&gt;</field>
<field name="snippetcode">
&lt;CODE&gt;
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
&lt;/CODE&gt;</field>
</data>
</node>
