RSSAmplifier

Volution Notes · May 4, 2020

Properly sorting FQDNs in Bash

0
Sign in to vote or save

notes.volution.ro

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. the google, microsoft, etc.;
  • then sort FQDNs based on c, i.e. the www, 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 sed basically transforms c.b.a into a|b|c|;
  • the second sed transforms that back into c.b.a.;
  • the third sed cuts the final .;
  • LC_ALL=C is required to make sure that sort actually applies lexicographical sorting, and not some "locale" specific one;

Read the original on notes.volution.ro

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.