SPUG: Re: Perl new-bee...

Ryan Erwin ryan at dbedge.com
Thu Apr 13 19:42:42 CDT 2000


You need to do a little bit of reading about Regular Expressions
and DBI.  You'll need to grab the DBI module from CPAN,
http://www.cpan.org, also See:
$ perldoc perlre
$ perldoc DBI

Without looking too hard at what you have, you'll probably want
something like:

#!/usr/bin/perl -w

use strict;
# use DBI; when you are ready ;)

sub section1{
    # stuff each 'element' in the line into an
    # item in the 'data' array @data
    my @data = split "\w+";
    # do section 1 specific stuff
}

sub section2{
    # do section 2 specific stuff
}

# yadda, yadda, yadda...

while (1) {
    open FILE, "/var/log/myLogFile";
    my $section;
    while (<FILE>) {
        # check which section the line is in, and call
        # a subroutineX, corresponding to that section.
        # don't forget that our buddy $_ is the default
        # var for most functions...
        section1 if $section = 1 || m#^Section 1\w*$#;
        section2 if $section = 2 || m#^Section 2\w*$#;
        # yadda, yadda, yadda...
    }
}
sleep 3000;    # wait for a while...

Of course, this is just a start and if your final
product ends up looking like this, you did something
dramatically wrong 8-}.  Notice that we are explicitly
comparing each line to see which section it is in, and
we are comparing it repeatedly.  This would be *very*
inefficient for something you are going to have running
all of the time, but seeing as how you are a newbie, it
should be easier to understand when things are verbose...

One place you will have to be particularly careful is if
for some reason one of the fields is 'missing' in one
data dump.  For example:
1    10    43    32    32    43    10021
12   43    84          23    23    23

If you are just using 'split #\w+#' then you will have
all of your values after '84' shifted one column... if
the column the data is in is relevant, then you will need
to figure out how to deal with that...

Good Luck 8)

BTW: while your out picking up books, I highly recommend:
- Mastering Regular Expressions
- Effective Perl Programming
- Object Oriented Perl

---
Ryan Erwin
ESPUG Emperor
----- Original Message -----
From: "Ricardo Cardona" <ricardo_cardona at hotmail.com>
> Here is my pet project;
> I’ve got an ASCI file that I what to parse and manipulate at half hour
> increments.  It looks something like this;  I added the > just to separate
> the different sections
>
> >Section 1
> CLASS: WEDAY
> START:2000/02/02 00:00:00 WED; STOP: 2000/02/03 00:00:00 THU;
> SLOWSAMPLES:       864 ; FASTSAMPLES:       8640 ;
>
> >Section 2
>        KEY (COMMON_LANGUAGE_NAME)
>        INFO (OM2TRKINFO)
>           INCATOT   PRERTEAB     INFAIL    NATTMPT
>          NOVFLATB      GLARE    OUTFAIL    DEFLDCA
>              DREU       PREU        TRU        SBU
>               MBU   OUTMTCHF    CONNECT     TANDEM
>               AOF        ANF       TOTU     ANSWER
>           INVAUTH    BLKCTRK      MAXBU    TRU2WIN
>           NCTPASS    NCTFAIL    ACCCONG
>
> >Section 3
>     52 FLKWLXCB
>     OG     1     1
>                 0          0          0          0
>                 0          0          0          0
>                 0          0          0          0
>               864          0          0          0
>                 0          0        864          0
>                 0          0         48          0
>                 0          0          0
> >Section 4
>     54 LDEKDD054I7
>     2W    72    72
>              9378          0         47          0
>                 0          0          0          0
>                 0          0      14702          0
>                 0          0          0       9327
>                 0          0      14702          0
>                 0          0       1090      14702
>                 0          0          0
>
>
> I get this file every half hour 24/7, the above clip is only the first
page
> from this file, and it’s actually about 5000 lines long and contains data
> for about 950 circuits.
>
>      Section 1 is just the header for the file and contains info that is
not
> important to me at the moment.
>      Section 2 contains the field headers for the following repetitive
> sections.
>      Section 3 contains the actual data fields for a particular circuit
> (example circuit # ‘54 LDEKDD054I7’ from section 4.)
>
> What I’m looking to do is this;  I trying to figure out a way to parse
this
> file and place the data fields into a database/file with the field headers
> from section 2.  From my limited knowledge of Perl I figure that I can
place
> this data into an associative array using the circuit ID # as the name of
> the array and the fields headers as the key for that array.  (Ideally I
> would like to create a database table for each of these circuits.  This
> database table would be updated with a new record extracted from each half
> hour file  (something for the future)).    Problem is this, based on what
I
> have learned from my reading, I’m not sure how to parse this file and
> extract the data fields and place them into a variable/list/array.  From
> what I’ve read Perl is great at manipulating text files but I don’t have
any
> examples of how to do what I’m looking to do.  By the way, the data fields
> are not delimited (although I’m told that I can receive them in a
delimited
> format) they are actually a screen dump from a terminal server.
>
> I would like to put these files into this a data base format so that I may
> have a run simple ratio calculations on the different fields.  For
instance,
> INFAIL by NATTMPT, in the future I would like to display and change this
> ratio info graphically using a Perl module or Tcl/Tk.
>
> Hey guys I know that you must be busy with your day-to-day stuff but any
> assistance/ guidance/support you can offer will be greatly appreciated.
> Please forward and any comments or help to ricardo_cardona at hotmail.com
>
> Thanks.
> RC


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
           Email to majordomo at pm.org: "action" spug-list your_address





More information about the spug-list mailing list