Parsing data file, need help with the logic

Discussion in 'Perl Misc' started by guser@packetstorm.org, Jun 27, 2006.

  1. Guest

    I have a set of files that get generated from a login script that I
    need to integrate into our database.

    The format in each file is like this

    Machine:blah
    User:foo
    Domain:foo.bar
    SN:nnnnnnn
    Asset:nnnnnnnnnnnnn
    OS:blah
    SP:blah

    Windows IP Configuration

    Host Name . . . . . . . . . . . . : blah
    Primary Dns Suffix . . . . . . . : foo.bar
    Node Type . . . . . . . . . . . . : Hybrid
    IP Routing Enabled. . . . . . . . : No
    WINS Proxy Enabled. . . . . . . . : No
    DNS Suffix Search List. . . . . . : foo.bar
    foo.bar
    foo.bar

    Ethernet adapter Wireless Network Connection:

    Media State . . . . . . . . . . . : Media disconnected
    Description . . . . . . . . . . . : blah


    Ethernet adapter Local Area Connection:

    Connection-specific DNS Suffix . : blah
    Description . . . . . . . . . . . : blah Gigabit Integrated
    Controller
    Physical Address. . . . . . . . . : mac
    Dhcp Enabled. . . . . . . . . . . : Yes
    Autoconfiguration Enabled . . . . : Yes
    IP Address. . . . . . . . . . . . : n.n.n.n
    Subnet Mask . . . . . . . . . . . : n.n.n.n
    Default Gateway . . . . . . . . . : n.n.n.n
    DHCP Server . . . . . . . . . . . : n.n.n.n
    DNS Servers . . . . . . . . . . . : n.n.n.n
    n.n.n.n
    Primary WINS Server . . . . . . . : n.n.n.n
    Secondary WINS Server . . . . . . : n.n.n.n
    n.n.n.n
    Lease Obtained. . . . . . . . . . : Tuesday, June 27, 2006
    7:05:51 AM
    Lease Expires . . . . . . . . . . : Wednesday, June 28, 2006
    7:05:51 AM

    Ethernet adapter Network Connect Adapter:


    Description . . . . . . . . . . . : blah Virtual Adapter
    Physical Address. . . . . . . . . : mac



    Here is the logic I am trying to convert to code:

    while (<FILE>)
    do until $_ =~ /Windows IP Configuration/
    get Machine:blah
    User:foo
    Domain:foo.bar
    SN:nnnnnnn
    Asset:nnnnnnnnnnnnn
    OS:blah
    SP:blah
    }
    do until $_ =~ /Ethernet adapter/
    get Host Name . . . . . . . . . . . . : blah
    Primary Dns Suffix . . . . . . . : foo.bar
    Node Type . . . . . . . . . . . . : Hybrid
    IP Routing Enabled. . . . . . . . : No
    WINS Proxy Enabled. . . . . . . . : No
    DNS Suffix Search List. . . . . . : foo.bar
    foo.bar
    foo.bar

    #at this point i need some help as I have N interface chunks to loop
    through before reaching the end of file.


    any suggestions?

    thanks.
    , Jun 27, 2006
    #1
    1. Advertising

  2. dh Guest

    Hi,
    try the following:
    while (<>){
    ($hostname)= /.+:(.*)/ if (/Host Name/);
    ($primdnssuf)= /.+:(.*)/ if (/Primary Dns Suffix/);
    ($mediastate)= /.+:(.*)/ if (/Media State/);
    ($physaddress)= /.+:(.*)/ if (/Physical Address/);
    ($ipaddress)= /.+:(.*)/ if (/IP Address/);
    };

    Daniel


    wrote:
    > I have a set of files that get generated from a login script that I
    > need to integrate into our database.
    >
    > The format in each file is like this
    >
    > Machine:blah
    > User:foo
    > Domain:foo.bar
    > SN:nnnnnnn
    > Asset:nnnnnnnnnnnnn
    > OS:blah
    > SP:blah
    >
    > Windows IP Configuration
    >
    > Host Name . . . . . . . . . . . . : blah
    > Primary Dns Suffix . . . . . . . : foo.bar
    > Node Type . . . . . . . . . . . . : Hybrid
    > IP Routing Enabled. . . . . . . . : No
    > WINS Proxy Enabled. . . . . . . . : No
    > DNS Suffix Search List. . . . . . : foo.bar
    > foo.bar
    > foo.bar
    >
    > Ethernet adapter Wireless Network Connection:
    >
    > Media State . . . . . . . . . . . : Media disconnected
    > Description . . . . . . . . . . . : blah
    >
    >
    > Ethernet adapter Local Area Connection:
    >
    > Connection-specific DNS Suffix . : blah
    > Description . . . . . . . . . . . : blah Gigabit Integrated
    > Controller
    > Physical Address. . . . . . . . . : mac
    > Dhcp Enabled. . . . . . . . . . . : Yes
    > Autoconfiguration Enabled . . . . : Yes
    > IP Address. . . . . . . . . . . . : n.n.n.n
    > Subnet Mask . . . . . . . . . . . : n.n.n.n
    > Default Gateway . . . . . . . . . : n.n.n.n
    > DHCP Server . . . . . . . . . . . : n.n.n.n
    > DNS Servers . . . . . . . . . . . : n.n.n.n
    > n.n.n.n
    > Primary WINS Server . . . . . . . : n.n.n.n
    > Secondary WINS Server . . . . . . : n.n.n.n
    > n.n.n.n
    > Lease Obtained. . . . . . . . . . : Tuesday, June 27, 2006
    > 7:05:51 AM
    > Lease Expires . . . . . . . . . . : Wednesday, June 28, 2006
    > 7:05:51 AM
    >
    > Ethernet adapter Network Connect Adapter:
    >
    >
    > Description . . . . . . . . . . . : blah Virtual Adapter
    > Physical Address. . . . . . . . . : mac
    >
    >
    >
    > Here is the logic I am trying to convert to code:
    >
    > while (<FILE>)
    > do until $_ =~ /Windows IP Configuration/
    > get Machine:blah
    > User:foo
    > Domain:foo.bar
    > SN:nnnnnnn
    > Asset:nnnnnnnnnnnnn
    > OS:blah
    > SP:blah
    > }
    > do until $_ =~ /Ethernet adapter/
    > get Host Name . . . . . . . . . . . . : blah
    > Primary Dns Suffix . . . . . . . : foo.bar
    > Node Type . . . . . . . . . . . . : Hybrid
    > IP Routing Enabled. . . . . . . . : No
    > WINS Proxy Enabled. . . . . . . . : No
    > DNS Suffix Search List. . . . . . : foo.bar
    > foo.bar
    > foo.bar
    >
    > #at this point i need some help as I have N interface chunks to loop
    > through before reaching the end of file.
    >
    >
    > any suggestions?
    >
    > thanks.
    >
    dh, Jun 27, 2006
    #2
    1. Advertising

  3. Guest

    dh wrote:
    > Hi,
    > try the following:
    > while (<>){
    > ($hostname)= /.+:(.*)/ if (/Host Name/);
    > ($primdnssuf)= /.+:(.*)/ if (/Primary Dns Suffix/);
    > ($mediastate)= /.+:(.*)/ if (/Media State/);
    > ($physaddress)= /.+:(.*)/ if (/Physical Address/);
    > ($ipaddress)= /.+:(.*)/ if (/IP Address/);
    > };
    >
    > Daniel
    >


    thanks, thats alot cleaner than what I was doing but did not address
    (or I am missing the clue) what to do with multiple adapter records in
    the same file.

    As long as the while loop is true and if a file has say 3 network
    adapter entries, the variables for the adapter parts will get
    overwritten. Or worse, if the local adapter is first and a vpn adapter
    is next, the above could overwrite the physaddress with the vpn adapter
    mac.
    , Jun 27, 2006
    #3
  4. Guest

    ah i got it now, something like this

    while (<FILE>) {
    ($hostname) = /.+:(.*)/ if (/Host Name/);
    ($primdnssuf) = /.+:(.*)/ if (/Primary Dns Suffix/);
    ($mediastate) = /.+:(.*)/ if (/Media State/);
    ($physaddress)= /.+:(.*)/ if (/Physical Address/);
    ($ipaddress) = /.+:(.*)/ if (/IP Address/);
    ($description) = /.+:(.*)/ if (/Description/);



    if ($mediastate ne '') { push (@mediastate,$mediastate); }
    if ($physaddress ne '') { push (@physaddress,$physaddress); }
    if ($description ne '') { push (@description,$description); }

    $hostname = '';
    $primdnssuf = '';
    $mediastate = '';
    $physaddress = '';
    $description = '';
    }

    then i get the physaddress array size and use that to loop for each
    adapter array entry I stored.
    , Jun 27, 2006
    #4
  5. Ben Morrow Guest

    Quoth :
    > ah i got it now, something like this
    >
    > while (<FILE>) {
    > ($hostname) = /.+:(.*)/ if (/Host Name/);
    > ($primdnssuf) = /.+:(.*)/ if (/Primary Dns Suffix/);
    > ($mediastate) = /.+:(.*)/ if (/Media State/);
    > ($physaddress)= /.+:(.*)/ if (/Physical Address/);
    > ($ipaddress) = /.+:(.*)/ if (/IP Address/);
    > ($description) = /.+:(.*)/ if (/Description/);
    >
    >
    >
    > if ($mediastate ne '') { push (@mediastate,$mediastate); }
    > if ($physaddress ne '') { push (@physaddress,$physaddress); }
    > if ($description ne '') { push (@description,$description); }
    >
    > $hostname = '';
    > $primdnssuf = '';
    > $mediastate = '';
    > $physaddress = '';
    > $description = '';
    > }
    >
    > then i get the physaddress array size and use that to loop for each
    > adapter array entry I stored.


    I would write this more like

    my (@mediastate, @physaddress, @description);

    while (<FILE>) {

    my ($k, $v) = /(.+?):(.*)/ or next;

    for ($k) {
    /Media State/ and push @mediastate, $v;
    /Physical Address/ and push @physaddress, $v;
    /Description/ and push @description, $v;
    }
    }

    as I think it puts things in a better order (first we check the basic
    syntax, then the key). If you're not stuck with those variables, I'd
    grab all the info like

    my %status;

    while (<FILE>) {
    /\s* (.+?) [.\s]* : (.*)/x or next;
    push @{ $status{$1} }, $2;
    }

    and then you've got your data in @{$status{'Media State'}} &c.

    Ben

    --
    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.
    Groucho Marx
    Ben Morrow, Jun 27, 2006
    #5
  6. <> wrote:

    > if ($mediastate ne '') { push (@mediastate,$mediastate); }



    I think that is more nicely written this way:

    push @mediastates, $mediastate if length $mediastate;


    (Arrays names should be in plural form too.)

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 27, 2006
    #6
  7. Glenn Jackman <> wrote:


    > if (/Media State.+:(.*)/) {
    > $mediastate = $1;
    > push @mediastate, $mediastate;
    > }



    No need to copy it to a temporary variable here.

    Once you eliminate the unnecessary variable, it is not homely
    as a one liner:

    push @mediastate, $1 if /Media State.+:(.*)/;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jun 27, 2006
    #7
    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. TechGurl

    Need help with my logic

    TechGurl, Oct 9, 2006, in forum: Java
    Replies:
    12
    Views:
    458
    TechGurl
    Oct 16, 2006
  2. Rex
    Replies:
    2
    Views:
    283
  3. Replies:
    2
    Views:
    393
  4. spike
    Replies:
    8
    Views:
    1,437
    Steve Holden
    Feb 9, 2010
  5. Replies:
    5
    Views:
    68
    Chris Angelico
    May 14, 2014
Loading...

Share This Page