By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

Parse text file

P: n/a
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
at http://www.TitanNews.com <<<<

-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


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

Tyaan <as**@asdf.com> wrote in message news:<95********************************@4ax.com>. ..
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.
Jul 19 '05 #2

P: n/a
On 20 Nov 2003 11:54:27 -0800, rj******@shell.com (Roy Johnson) wrote:
Your message will reach more people in comp.lang.perl.misc or
perl.beginners. This newsgroup is no longer a valid public newsgroup.

Tyaan <as**@asdf.com> wrote in message news:<95********************************@4ax.com>. ..
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.


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
at http://www.TitanNews.com <<<<

-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.