[snippet] Properly sorting FQDNs in Bash

by Ciprian Dorin Craciun (https://volution.ro/ciprian) on 

How to properly sort a list of FQDNs in Bash, first by TLD, then by domain, then by sub-domain and so on.

// permanent-link // Lobsters // HackerNews // index // RSS





Say you have a long list of FQDNs that you want to "properly" sort from the TLD to the leafs, the following snippet, based only on sed and sort, will efficiently solve this task.

By "properly" sort I mean applying the following rules:

Basically it is a lexicographical sorting in "reverse".


...
| LC_ALL=C sed -r \
        -e ': l' \
        -e 's#(([^.|]+\|)*)(([^.|]+\.)+)*(([^.|]+)\.?)$#\1\6|\3#' \
        -e 't l' \
| LC_ALL=C sort \
        -k 1,1 -k 2,2 -k 3,3 \
        -k 4,4 -k 5,5 -k 5,5 \
        -k 6 \
        -t '|' \
| LC_ALL=C sed -r \
        -e ': l' \
        -e 's#(([^.|]+\.)*)(([^.|]+\|)+)*(([^.|]+)\|?)$#\1\6.\3#' \
        -e 't l' \
| LC_ALL=C sed -r -e 's#\.$##' \
...

Observations: