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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify a perl line to parse out and output to another format
# 1  
Old 06-01-2012
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

Code:
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 ):

Code:
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:
Code:
ptrrecord,acf0928r06.homeoffice.anfcorp.com,,10.11.161.143,,,False,143.161.11.10.in-addr.arpa,,1200,default

convert it to:
Code:
conf zone 10.11.161.0/24 del PTR 10.11.161.143 acf0928r06.homeoffice.anfcorp.com


Code:
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

Last edited by richsark; 06-01-2012 at 11:26 PM.. Reason: Please wrap data and scripts/commands with CodeTags
# 2  
Old 06-02-2012
try this
Code:
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

# 3  
Old 06-02-2012
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.
# 4  
Old 06-02-2012
Code:
 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
This User Gave Thanks to pravin27 For This Post:
# 5  
Old 06-02-2012
Cool. I did not know ? Was a sequence. I guess you learn something new every other day.

Thanks, I will it and revert.
# 6  
Old 06-02-2012
In awk, try:
Code:
awk -F, '{n=$4; sub(/\.[0-9]*$/,x,n); print "conf zone", n ".0/24 del PTR", $4, $2}' file


Last edited by Scrutinizer; 06-02-2012 at 03:19 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 06-02-2012
Thank you both !!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to parse this file using awk and output in CSV format?

My source file looks like this: Cust-Number = "101" Cust-Name="Joe" Cust-Town="London" Cust-hobby="tennis" Cust-purchase="200" Cust-Number = "102" Cust-Name="Mary" Cust-Town="Newyork" Cust-hobby="reading" Cust-purchase="125" Now I want to parse this file (leaving out hobby) and... (10 Replies)
Discussion started by: Balav
10 Replies

2. Solaris

Grant unprivileged user rights to see the output of echo|format but not modify disks

anyone have any idea how do to this with auth_attr? I suspect if I grant him solaris.device.:RO::Device Allocation::help=DevAllocHeader.html that will work but I'm unsure. Just looking for a second opinion. (10 Replies)
Discussion started by: os2mac
10 Replies

3. Shell Programming and Scripting

modify ls -l (long listing format output) strictly using SED only straightforward goalhard 4 me doh

Below is a sample out of ls -l which I would like to rearrange or modify by field numbers for example I successfully managed to disect using simple paragraph however for ls -l I can't divide the rows or fields by field number. Successful modification by fields using SED sample: $ sed -e... (1 Reply)
Discussion started by: wolf@=NK
1 Replies

4. Shell Programming and Scripting

parse output line using bash

hi, i have the followiing scenario where by i am parsing teh following output using cut -d like so #!/bin/bash output="ABCTable| ------------------| | ------------------| code | name | amount |" col1= $output | cut -d'|' -f5 col2= $output | cut -d'|'... (1 Reply)
Discussion started by: nano2
1 Replies

5. Shell Programming and Scripting

Perl script to parse output and print it comma separated

I need to arrange output of SQL query into a comma separated format and I'm struggling with processing the output... The output is something like this: <Attribute1 name><x amount of white spaces><Atribute value> <Attribute2 name><x amount of white spaces><Atribute value> <Attribute3... (2 Replies)
Discussion started by: Juha
2 Replies

6. Shell Programming and Scripting

Perl Parse word from command output

Hello, I used the following script to conect to cisco router: #!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Opsware::NAS::Connect; my($host, $port, $user, $pass) = ('localhost','$tc_proxy_telnet_port$','$tc_user_username$','$tc_user_password$'); my $device =... (5 Replies)
Discussion started by: ahmed_zaher
5 Replies

7. Shell Programming and Scripting

perl parse line

Dear all anyone willling to help me..i have try so many time but still failed to get the ip address for line when i print the line is like below Connected to 192.168.1.13 #!/usr/local/bin/perl foreach $line(@lines){ if ($line =~ /connected to/) { $line=~/connected to(.*?) /; ... (2 Replies)
Discussion started by: netxus
2 Replies

8. Shell Programming and Scripting

Need help to modify perl script: Text file with line and more than 1 space

Dear Friends, I am beginner in Perl and trying to find the problem in a script. Kindly help me to modify the script. My script is not giving the output for the last field and followed text (LA: Language English). Input file & script as follows: Input file: Thu Mar 19 2:34:14 EDT 2009 STC... (3 Replies)
Discussion started by: srsahu75
3 Replies

9. Shell Programming and Scripting

Format a line for output

Hi all I have a problem which requires me to produce a line output in a specified format. I have looked around and see that there is typeset which would do the trick to pad a variable and as such ensure that its start position in a line is always the same. I have tried to use this, however it... (4 Replies)
Discussion started by: jhansrod
4 Replies

10. Shell Programming and Scripting

Perl: Search for string then parse next line

Hi All, I have a file that I need to be able to find a pattern match on one line then parse data on the next or subsequent lines - I will know which line needs to be parsed beforehand. This is what I currently have: while (<COMMAND_OUT>) { if ($_ =~ m/TEST/) { ... (4 Replies)
Discussion started by: pondlife
4 Replies
Login or Register to Ask a Question