drewrockshard
drewrockshard

Reputation: 2071

Extract IP and Pointer Record from zone file

I'm reading in a file which is part of an AXFR file that I have exported to a txt file. Basically, I cat out the file, grep out ONLY the PTR records (I'm only interested in these right now). I've tried using awk, but I'm limited in my overall knowledge of awk and don't plan on learning it extensively yet. Here's is what the file kind of looks like:

1.0.0.10.in-addr.arpa. 1800 IN  PTR name-1.something.something.else.  
2.0.0.10.in-addr.arpa. 1800 IN  PTR name-2.something.something.else.  
3.0.0.10.in-addr.arpa. 1800 IN  PTR name-3.something.something.else.  
4.0.0.10.in-addr.arpa. 1800 IN  PTR name-4.something.something.else.  
5.0.0.10.in-addr.arpa. 1800 IN  PTR name-5.something.something.else. 

What I need the output to be is the IP address (which needs to be reversed), the PTR, and then the actual record it is pointed to, so the output from what is above would look like:

10.0.0.1    PTR     name-1.something.something.else.  
10.0.0.2    PTR     name-1.something.something.else.  
10.0.0.3    PTR     name-1.something.something.else.  
10.0.0.4    PTR     name-1.something.something.else.  
10.0.0.5    PTR     name-1.something.something.else. 

Is this something that I can do with awk, and if so, how? I'm having troubles getting it to work and I'm at a standstill with how I would logically get this working.

Upvotes: 2

Views: 903

Answers (3)

Endoro
Endoro

Reputation: 37589

sed only:

sed 's/\([0-9]*\).\([0-9]*\).\([0-9]*\).\([0-9]*\).*\(PTR\)/\4.\3.\2.\1\t\5\t/' file

Upvotes: 0

Jims
Jims

Reputation: 68

... | perl -ne '
while (<STDIN>)
{
    my ($ip, $record) = (split (/\s+/))[0, 4];
    $ip =~ s/\Q.in-addr.arpa.\E$//;
    my @tks = split (/\./, $ip);
    print join (".", reverse (@tks)), " PTR $record\n";
}
'

Upvotes: 0

Barmar
Barmar

Reputation: 782693

You don't need to use cat or grep, since awk can read from a file and do pattern matching.

awk '/PTR/ { split($0, ip, /\./);
             printf("%s.%s.%s.%s\tPTR\t%s\n", ip[4], ip[3], ip[2], ip[1], $NF);
           }' filename

Upvotes: 2

Related Questions