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:
- given a FQDN in the form
c.b.a, - first sort FQDNs based on
a, i.e. the DNS label closer to the root, usually a TLD like.com,.net, etc.; - then sort FQDNs based on
b, i.e. thegoogle,microsoft, etc.; - then sort FQDNs based on
c, i.e. thewww,app,api, etc.; - and so on;
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:
- the first
sedbasically transformsc.b.aintoa|b|c|; - the second
sedtransforms that back intoc.b.a.; - the third
sedcuts the final.; LC_ALL=Cis required to make sure thatsortactually applies lexicographical sorting, and not some "locale" specific one;