Parse text file

T

Tyaan

Hi.. I'm a perl noob need to know how to write a script to parse a
file containing one to four of the following blocks of text? I then
want to print the results in a format showing the memory size (128)
for each device that was found?

Structure: Memory Device (Type 17)
Type: 17
Length: 15h
Handle: 0024h (36t)
Memory Array Handle: 0021
Memory Error Information Handle: FFFE
Total Width: 64 (0040h)
Data Width: 64 (0040h)
Size: 128 (0080h)
Form Factor: DIMM
Device Set: 0001h
Device Locator: DIMM 0
Bank Locator: BANK 0
Memory Type: SDRAM
Type Detail: 0080h
Serial Number:

For example, say the SMBIOS dump contained 2 of the above blocks and I
want to print something like:

myhost 128 256 x x

Would I use an array to store the 4 memory values? I tried something
like the following.. .Am I on the right track? I just need help
with how to parse the file and extract the memory sizes into some type
of variable.

while (<INFILE>)
{
if(/\(Type 17\)/)
{
while(<INFILE>)
{ if(/Size/)
{ # Assign size to array
$memory=~s/^\s*Size\:\s*//;
}
if(/Serial/)
{ next; }
}

}
}





.................................................................
Posted via TITANnews - Uncensored Newsgroups Access-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-
 
R

Roy Johnson

Your message will reach more people in comp.lang.perl.misc or
perl.beginners. This newsgroup is no longer a valid public newsgroup.

Tyaan said:
Would I use an array to store the 4 memory values?

Probably. Is the size the only thing you want to keep?

my @sizes = ();
while (<INFILE>) {
push(@sizes, $') if /^\s*Size\:\s*/;
}
print join("\t", @sizes), "\n";
while (<INFILE>)
{
if(/\(Type 17\)/)
{
while(<INFILE>)

This is definitely the wrong track. You don't want to have multiple
nested while()s reading from the same file handle.
 
T

Tyaan

Your message will reach more people in comp.lang.perl.misc or
perl.beginners. This newsgroup is no longer a valid public newsgroup.



Probably. Is the size the only thing you want to keep?

my @sizes = ();
while (<INFILE>) {
push(@sizes, $') if /^\s*Size\:\s*/;
}
print join("\t", @sizes), "\n";


This is definitely the wrong track. You don't want to have multiple
nested while()s reading from the same file handle.

I re-wrote it like this. Is this a more "proper" way of doing it?

open(INFILE, "smbios.txt");
read(INFILE, $_, -s "smbios.txt");
close(INFILE)
while (/(\(Type\s*\d*\))(.*?)(Structure)/s)
{
# Save strings
$temp=$';
$type=$1;
$value=$2;

# If a memory block is found, parse it.
if($type =~ /Type 17\)/)
{
$value=~/(.*?)(Size:)(\s*)(\d*)/s;
$memcount++;
@memory[$memcount]=$4;
}

$_=$temp;
}



.................................................................
Posted via TITANnews - Uncensored Newsgroups Access-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top