Skip to content

Instantly share code, notes, and snippets.

@gwire
Created April 22, 2023 16:23
Show Gist options
  • Save gwire/96d67b5f71dfd153c57cfa9f6b4d48df to your computer and use it in GitHub Desktop.
Save gwire/96d67b5f71dfd153c57cfa9f6b4d48df to your computer and use it in GitHub Desktop.
Sorting a list of in-addr.arpa values

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
./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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment