Modify a perl line to parse out and output to another format

Hey there...

I am looking for a way to take the below contents ( small excerpt) of this file called PTR.csv

ptrrecord,0000002e0cc0.homeoffice.anfcorp.com,,10.11.191.62,,,False,62.191.11.10.in-addr.arpa,,302400,default
ptrrecord,dmx-110383.homeoffice.anfcorp.com,,10.11.50.58,,,False,58.50.11.10.in-addr.arpa,,302400,default
ptrrecord,00000029263f.homeoffice.anfcorp.com,,10.11.200.62,,,False,62.200.11.10.in-addr.arpa,,302400,default
ptrrecord,0000002f7c2a.homeoffice.anfcorp.com,,10.11.85.190,,,False,190.85.11.10.in-addr.arpa,,302400,default
ptrrecord,hol0213r06.homeoffice.anfcorp.com,,10.11.50.143,,,False,143.50.11.10.in-addr.arpa,,1200,default
ptrrecord,acf0928r06.homeoffice.anfcorp.com,,10.11.161.143,,,False,143.161.11.10.in-addr.arpa,,1200,default
ptrrecord,hol0113r04.homeoffice.anfcorp.com,,10.10.2.13,,,False,13.2.10.10.in-addr.arpa,,1200,default
ptrrecord,dhcp-10-11-81-189.homeoffice.anfcorp.com,,10.11.81.189,,,False,189.81.11.10.in-addr.arpa,,302400,default
ptrrecord,dhcp-10-11-81-188.homeoffice.anfcorp.com,,10.11.81.188,,,False,188.81.11.10.in-addr.arpa,,302400,default
ptrrecord,dhcp-10-11-58-59.homeoffice.anfcorp.com,,10.11.58.59,,,False,59.58.11.10.in-addr.arpa,,302400,default
ptrrecord,dhcp-10-11-58-57.homeoffice.anfcorp.com,,10.11.58.57,,,False,57.58.11.10.in-addr.arpa,,302400,default
ptrrecord,dhcp-10-11-58-58.homeoffice.anfcorp.com,,10.11.58.58,,,False,58.58.11.10.in-addr.arpa,,302400,default
ptrrecord,dhcp-10-10-155-58.homeoffice.anfcorp.com,,10.10.155.58,,,False,58.155.10.10.in-addr.arpa,,302400,default

All i need are the ones that fall into 10.8.x.x to 10.14.x.x ones

and convert them ( Example ):

conf zone 10.8.16.0/24 del PTR 10.8.16.10 ruh0127r01.homeoffice.anfcorp.com

conf zone 10.11.133.0/24 del PTR 10.11.133.181 dhcp-10-11-133-181.homeoffice.anfcorp.com

Now the leading network and mask can be all /24's so if we take line 11
from:

ptrrecord,acf0928r06.homeoffice.anfcorp.com,,10.11.161.143,,,False,143.161.11.10.in-addr.arpa,,1200,default

convert it to:

conf zone 10.11.161.0/24 del PTR 10.11.161.143 acf0928r06.homeoffice.anfcorp.com
perl -aF, -ne 'printf "conf zone %2\$s del PTR %s,,,$F[3]\n",split/\./,$F[1],2 if /^ptrrecord/ &&/\b10\.([89]|1[01234])\.\d+\.\d+/' PTR.csv

Thanks

try this

perl -ne 'if (/(.*?),(.*?),,(10)\.(1[0-4]|[89])\.([01]?\d\d|2[0-4]\d|25[0-5])\.([01]\d\d|2[0-4]\d|25[0-5])/) {
print "conf zone ",$3,"\.",$4,"\.",$5,".0\/24 del PTR ",$3,"\.",$4,"\.",$5,"\.",$6," ",$2,"\n";}' filename

Ok. I made a mistake in my example above. I corrected below.

from:
ptrrecord,dhcp-10-11-58-57.homeoffice.anfcorp.com,,10.11.58.57,,,False,57.58.11.10.in-addr.arpa,,302400,default

convert it to:
conf zone 10.11.58.0/24 del PTR 10.11.58.57 dhcp-10-11-58-57.homeoffice.anfcorp.com

not sure if your expression needs adjustments.

 perl -ne 'if (/(.*?),(.*?),,(10)\.(1[0-4]|[89])\.([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])/) {
print "conf zone ",$3,"\.",$4,"\.",$5,".0\/24 del PTR ",$3,"\.",$4,"\.",$5,"\.",$6," ",$2,"\n";}' filename

I have missed ?.. Now it will work

1 Like

Cool. I did not know ? Was a sequence. I guess you learn something new every other day.

Thanks, I will it and revert.

In awk, try:

awk -F, '{n=$4; sub(/\.[0-9]*$/,x,n); print "conf zone", n ".0/24 del PTR", $4, $2}' file
1 Like

Thank you both !!!