Parsing data file, need help with the logic

G

guser

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.
 
D

dh

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
 
G

guser

dh said:
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.
 
G

guser

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.
 
B

Ben Morrow

Quoth (e-mail address removed):
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
 
T

Tad McClellan

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.)
 
T

Tad McClellan

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.+:(.*)/;
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top