text parse

D

david

Im trying to parse a file with data such as this:
(ip's changed for obvious reasons)

acl add table=ipaddr name=127.0.0.1 burb=Firewall \
comments='Firewall network object for DNS proxy ACL redirection' \
lastchangedby='root on 08/12/02 13:36:23'
acl add table=ipaddr name=127.1.1.1 burb=external \
lastchangedby='dlepage on 06/24/03 10:25:30'

I want the end output to look like:

acl add name=127.0.0.1 ipaddr=127.0.0.1 comments='Firewall network
object for DNS proxy ACL redirection'
acl add name=127.1.1.1 ipaddr=127.1.1.1 comments=

so basically I only want to capture the IP after the 'name=' value,
and anything in the 'comments' field, if at all. Here is what I have
so far, which works, but im so new to programming that I know my
coding stinks. Any suggestions to make this more efficient
appreciated.

#!/usr/bin/perl -w
use strict;

my ($ip,$comment);

open(IN, "<$ARGV[0]") || die "Cant open $!";
flock (IN, 1);

while( <IN> ) {
s/\\//g; # get rid of newlines

# match on acl's with comments
if (/^acl add table=ipaddr name=(\d+\.\d+\.\d+\.\d+) burb=(.+)
comments=
(.+)/) {
print "acl add name=$1 ipaddr=$1 comments=$3\n";
}

# else match without comments
elsif (/^acl add table=ipaddr name=(\d+\.\d+\.\d+\.\d+)
burb=(.+)$/) {
print "acl add name=$1 ipaddr=$1 comments=\n";
}
}
close(IN);

-d
 
D

david

Im trying to parse a file with data such as this:
(ip's changed for obvious reasons)

acl add table=ipaddr name=127.0.0.1 burb=Firewall \
comments='Firewall network object for DNS proxy ACL redirection' \
lastchangedby='root on 08/12/02 13:36:23'
acl add table=ipaddr name=127.1.1.1 burb=external \
lastchangedby='dlepage on 06/24/03 10:25:30'

I want the end output to look like:

acl add name=127.0.0.1 ipaddr=127.0.0.1 comments='Firewall network
object for DNS proxy ACL redirection'
acl add name=127.1.1.1 ipaddr=127.1.1.1 comments=

so basically I only want to capture the IP after the 'name=' value,
and anything in the 'comments' field, if at all. Here is what I have
so far, which works, but im so new to programming that I know my
coding stinks. Any suggestions to make this more efficient
appreciated.

#!/usr/bin/perl -w
use strict;

my ($ip,$comment);

open(IN, "<$ARGV[0]") || die "Cant open $!";
flock (IN, 1);

while( <IN> ) {
s/\\//g; # get rid of newlines

# match on acl's with comments
if (/^acl add table=ipaddr name=(\d+\.\d+\.\d+\.\d+) burb=(.+)
comments=
(.+)/) {
print "acl add name=$1 ipaddr=$1 comments=$3\n";
}

# else match without comments
elsif (/^acl add table=ipaddr name=(\d+\.\d+\.\d+\.\d+)
burb=(.+)$/) {
print "acl add name=$1 ipaddr=$1 comments=\n";
}
}
close(IN);

-d


I did find a bug with this. There are continuation lines for each
entry. Each line that I want to analyze should start with the 'add acl
table' - this current version will not properly grab data after the
continuation (\) line. Im trying to remove these now.

Ok, call me stupid. Since noone has anything to add, I thought id post
the solution I came up with.. It seems to work just fine..

use strict;

# check argvs
if (@ARGV != 1) {
print "Usage: perl acl.pl <filename>\n";
exit 666
}

# open and lock file
open(IN, "<$ARGV[0]") || die "Cant open \"$ARGV[0]\" : $!";
flock (IN, 1);
open(OUT, ">import.txt") || die "$!";
flock (IN, 2);

my ($line,$ip,$comment);

while (defined ($line = <IN> )) { # rejoin continuation lines
chomp $line;
if ($line =~ s/\\$//) {
$line .= <IN>;
redo unless eof(IN);
}

# match on lines with comments
if ($line =~ /^acl add table=ipaddr name=(\d+\.\d+\.\d+\.\d+)
bu
rb=(.+) comments=(.+) lastchangedby=(.*)$/) {
print "acl add name=$1 ipaddr=$1 comments=$3\n";
}

# else match lines without comments
elsif ($line =~ /^acl add table=ipaddr
name=(\d+\.\d+\.\d+\.\d+)
burb=(.+) lastchangedby=(.*)$/) {
print "acl add name=$1 ipaddr=$1 comments=\n";
}
}
close(IN);
close(OUT);

__DATA__
acl add table=ipaddr name=127.0.0.1 burb=Firewall \
comments='Firewall network object for DNS proxy ACL
redirection' \
lastchangedby='root on 08/12/02 13:36:23'
acl add table=ipaddr name=127.1.1.1 burb=external \
lastchangedby='dlepage on 06/24/03 10:25:30'

output:

acl add name=127.0.0.1 ipaddr=127.0.0.1 comments='Firewall network
object for DNS proxy ACL redirection'
acl add name=127.1.1.1 ipaddr=127.1.1.1 comments=

Thanks for all the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top