I have an extract from a forward DNS zone file, which I want to sort by ascending IP address. Before you mark this as a duplicate, please read on a short while, because this isn't about sorting IP addresses as such (sort -k5V would address that).
Here is a sample of the data:
esx01.example.com.      3600    IN      A       10.1.1.212
ilo01.example.com.      3600    IN      A       10.1.1.211
nas01.example.com.      3600    IN      A       10.1.1.101
pc001.example.com.      1200    IN      A       10.1.1.42
pc002.example.com.      1200    IN      A       10.1.1.52
pc003.example.com.      1200    IN      A       10.1.1.29
In this specific case I know I can sort by just the last octet, so this should be a straightforward application of sort.
The man page confirms that I can use -k with not only a field but also an offset within that field, and with an n numeric modifier
KEYDEFisF[.C][OPTS][,F[.C][OPTS]]for start and stop position, whereFis a field number andCa character position in the field; both are origin 1, and the stop position defaults to the line's end. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace.OPTSis one or more single-letter ordering options [bdfgiMhnRrV], which override global ordering options for that key.
The last octet conveniently starts at character offset eight within the fifth field, so my understanding is that this command should suffice:
sort -k5.8n /tmp/axfr.10.1.1
However, this does not sort my data at all. Empirically I find I need to start at field position 15 to sort this data in ascending numeric order as expected:
sort -k5.15n /tmp/axfr.10.1.1
pc003.example.com.      1200    IN      A       10.1.1.29
pc001.example.com.      1200    IN      A       10.1.1.42
pc002.example.com.      1200    IN      A       10.1.1.52
nas01.example.com.      3600    IN      A       10.1.1.101
ilo01.example.com.      3600    IN      A       10.1.1.211
esx01.example.com.      3600    IN      A       10.1.1.212
Why?



sort's-Voption is useful to sort quad-decimal IPv4 addresses.