text parse

Discussion in 'Perl Misc' started by david, Jul 15, 2003.

  1. david

    david Guest

    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
     
    david, Jul 15, 2003
    #1
    1. Advertising

  2. david

    david Guest

    (david) wrote in message news:<>...
    > (david) wrote in message news:<>...
    > > 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.
     
    david, Jul 16, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M. Lembo

    Parse Text File and Output to File

    John M. Lembo, Aug 1, 2003, in forum: Perl
    Replies:
    0
    Views:
    12,719
    John M. Lembo
    Aug 1, 2003
  2. Tyaan

    Parse text file

    Tyaan, Nov 19, 2003, in forum: Perl
    Replies:
    2
    Views:
    810
    Tyaan
    Nov 20, 2003
  3. Sacha Korell
    Replies:
    2
    Views:
    14,933
    Mattias Sjögren
    Sep 6, 2003
  4. Replies:
    19
    Views:
    1,137
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    190
    7stud --
    Feb 20, 2008
Loading...

Share This Page