Parse text file

Discussion in 'Perl' started by Tyaan, Nov 19, 2003.

  1. Tyaan

    Tyaan Guest

    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=-
    Tyaan, Nov 19, 2003
    #1
    1. Advertising

  2. Tyaan

    Roy Johnson Guest

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

    Tyaan <> wrote in message news:<>...
    > 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.
    Roy Johnson, Nov 20, 2003
    #2
    1. Advertising

  3. Tyaan

    Tyaan Guest

    On 20 Nov 2003 11:54:27 -0800, (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 <> wrote in message news:<>...
    >> 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=-
    Tyaan, Nov 20, 2003
    #3
    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. John M. Lembo

    Parse Text File and Output to File

    John M. Lembo, Aug 1, 2003, in forum: Perl
    Replies:
    0
    Views:
    12,693
    John M. Lembo
    Aug 1, 2003
  2. Sacha Korell
    Replies:
    2
    Views:
    14,854
    Mattias Sjögren
    Sep 6, 2003
  3. Replies:
    19
    Views:
    1,098
    Daniel Vallstrom
    Mar 15, 2005
  4. John M. Lembo

    Parse Text File and Output to File

    John M. Lembo, Aug 1, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    148
    JS Bangs
    Aug 1, 2003
  5. Zhen Zhang

    parse a csv file into a text file

    Zhen Zhang, Feb 6, 2014, in forum: Python
    Replies:
    29
    Views:
    117
    Tim Chase
    Feb 6, 2014
Loading...

Share This Page