Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Parse text file

Reply
Thread Tools

Parse text file

 
 
Tyaan
Guest
Posts: n/a
 
      11-19-2003
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 (12
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=-

 
Reply With Quote
 
 
 
 
Roy Johnson
Guest
Posts: n/a
 
      11-20-2003
Your message will reach more people in comp.lang.perl.misc or
perl.beginners. This newsgroup is no longer a valid public newsgroup.

Tyaan <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> 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.
 
Reply With Quote
 
 
 
 
Tyaan
Guest
Posts: n/a
 
      11-20-2003
On 20 Nov 2003 11:54:27 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
>> 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=-

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
How to check for EOF (End of file) when using StreamReader to parse text file Sacha Korell ASP .Net 2 09-06-2003 02:59 PM
Parse Text File and Output to File John M. Lembo Perl Misc 2 08-01-2003 09:52 PM
Parse Text File and Output to File John M. Lembo Perl 0 08-01-2003 04:34 PM



Advertisments