Skip to content

Instantly share code, notes, and snippets.

@gwire
Created April 22, 2023 16:23

Revisions

  1. gwire created this gist Apr 22, 2023.
    33 changes: 33 additions & 0 deletions sorting_arpa_list.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    I have a process that outputs a list of `.in-addr.arpa` values. These consist of names with 3 to 6 labels.

    ```
    161.187.42.143.in-addr.arpa
    18.139.243.162.in-addr.arpa
    38.51.19.58.in-addr.arpa
    136.67.34.in-addr.arpa
    16.134.243.162.in-addr.arpa
    18.240.203.159.in-addr.arpa
    240.54.in-addr.arpa
    230.157.in-addr.arpa
    ```

    I wanted to sort this list, where the most significant number is the third label from the right hand side, and the order is numeric.

    I assumed I could do this with a simple command line unix tool, eg `sort`, but I couldn't figure out how to specify the columns as relative to the right hand side.

    My hacky solution:
    * reverse the components into a 6 column, space-separated, list
    * remove any empty columns at the beginning, such that column one is always `arpa`
    * Use numeric sort, keyed on columns 3,4,5,6
    * reverse the components into a 6 column, dot-separated, list
    * remove any empty columns at the beginning

    ```sh
    ./process \
    | awk -F. '{print $6,$5,$4,$3,$2,$1}' \
    | sed 's/^[\ ]*//' \
    | sort -k 3,3 -k 4,4 -k 5,5 -k 6,6 -g \
    | awk '{print $6,$5,$4,$3,$2,$1}' OFS=. \
    | sed 's/^[\.]*//' \
    | uniq
    ```