I am trying to awk information from a named file based on the first three octets of an IP. In this file the IP can be in either column 2 or column 4. If the IP is in column 2 I'd like to print what is in column 2 then what it is column 1. If the IP is in column 4 I'd like to print column 4 and column 1. (Column 1 in the server name).
This is an example of the output with all the columns printed:
awk /10.1.49/ /var/named/internal/Domain/abc.com
The output looks like this:
server01 IN A 10.1.49.29
server02 IN A 10.1.49.80
;globalname01.abc.com 10.1.49.60
;globalname02.abc.com 10.1.49.61
I want to sort by the IP column and then print IP and server name:
10.1.49.20 server01
10.1.49.60 globalname01.abc.com
10.1.49.61 globalname02.abc.com
10.1.49.80 server02
I do know how to use {print NF}
to get a column count, I just don't know how to use that information, such as if NF returns 4 then {print $4 $1}
but if NF returns 2 then {print $2 $1}
... and preferably sorted on top of that (but I can throw that data out to a file and then read it back in again to sort it if I need to).