Programming Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • crokett
    The Full Monte
    • Jan 2003
    • 10627
    • Mebane, NC, USA.
    • Ryobi BT3000

    #1

    Programming Help

    Well more design help. I've spent the last few hrs going through a text file. I'd like to be able to parse it. Below is a small sampling of the data I start with in the file. The blue text is specifically what I am interested in. There are several lines of this in a given file. Once I get this info then I search for it elswhere in the file and depending on the results of that search I want to spit out a formatted .csv file for Excel to read.

    I am thinking do the intial search and stuff each line into an array. Once the array is built I then traverse it and search elsewwhere in the file for those values. I can do this most easily in Perl - I'd have to dust off my books though. Can I allocate a dynamic array in Perl or would I first need to do a line count, then build the array and then reread to stuff the lines in?

    Type Pid COS PortName NodeName

    N 140900; 3;50:05:07:68:01:10:2d:fa;50:05:07:68:01:00:2d:fa;
    FC4s: FCP
    Fabric Port Name: 20:09:00:05:1e:36:11:48
    Permanent Port Name: 50:05:07:68:01:10:2d:fa
    Device type: Physical Target
    David

    The chief cause of failure in this life is giving up what you want most for what you want at the moment.
  • Alex Franke
    Veteran Member
    • Feb 2007
    • 2641
    • Chapel Hill, NC
    • Ryobi BT3100

    #2
    Perl arrays grow and shrink automagically as needed. This looks like a perfect application for regular expressions...

    Something like

    while ( /(([\da-f]{2}{7}([\da-f]{2}){2}/g )
    { /* match elsewhere, split, process, etc */ };


    okay... I tried and tried and tried to get rid of those smilies, but I failed. Sorry.
    Last edited by Alex Franke; 05-03-2007, 02:16 PM.
    online at http://www.theFrankes.com
    while ( !( succeed = try() ) ) ;
    "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

    Comment

    • Otter
      Senior Member
      • Nov 2003
      • 865
      • Cumming, GA, USA.
      • Delta Left Tilt UniSaw

      #3
      Originally posted by crokett
      Well more design help. I've spent the last few hrs going through a text file. I'd like to be able to parse it. Below is a small sampling of the data I start with in the file. The blue text is specifically what I am interested in. There are several lines of this in a given file. Once I get this info then I search for it elswhere in the file and depending on the results of that search I want to spit out a formatted .csv file for Excel to read.

      I am thinking do the intial search and stuff each line into an array. Once the array is built I then traverse it and search elsewwhere in the file for those values. I can do this most easily in Perl - I'd have to dust off my books though. Can I allocate a dynamic array in Perl or would I first need to do a line count, then build the array and then reread to stuff the lines in?

      Type Pid COS PortName NodeName

      N 140900; 3;50:05:07:68:01:10:2d:fa;50:05:07:68:01:00:2d:fa;
      FC4s: FCP
      Fabric Port Name: 20:09:00:05:1e:36:11:48
      Permanent Port Name: 50:05:07:68:01:10:2d:fa
      Device type: Physical Target
      Why into an array, I would keep it a a single string element asd find my first occurance of what ever dilimiter, once that is found I can find the end of the data fragment I wish to copy and place in a variable to pass to the search and extract routine I would write.

      nice thing about it requires 1 large string variable, if that, you could always make a temp copy of the file and work in raw acess mode (that is very slow)
      all local variables
      no house keeping needed

      just an idea.
      All men dream, but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dream with open eyes, to make it possible

      T.E. Lawrence

      Comment

      • radhak
        Veteran Member
        • Apr 2006
        • 3061
        • Miramar, FL
        • Right Tilt 3HP Unisaw

        #4
        Originally posted by Alex Franke
        okay... I tried and tried and tried to get rid of those smilies, but I failed. Sorry.
        you were trying this : ( /(([\da-f]{2}{7}([\da-f]{2}){2}/g )
        check 'disable smilies in text' below the edit window (misc options)

        but i am not sure David wants to search a generic string, i think its that particular string (in blue); if i am right, then it makes it even easier to search using regex : search for that long string then print it out onto an external file...
        It is the mark of an educated mind to be able to entertain a thought without accepting it.
        - Aristotle

        Comment

        • 25
          Established Member
          • Jan 2004
          • 294
          • League City, Tx, USA.
          • BT3100

          #5
          I made some assumptions on the format but heres one solution:
          Code:
          #!/usr/bin/perl -w
           
              #Don't leave home without it. 
              use strict;
           
              my $inFile = "/tmp/test.dat";
           
              #Turn slurp mode on, this allows you to read the entire
              #file in as a string.  Another option is to slurp as usual
              #into an array then join the array togather.
              local( $/, *FH ) ;
           
              open(FH, '< '.$inFile) or die $@, '  ',$!;
           
              my $file = <FH>;
           
              close(FH);
           
              #As long as the regular expression finds a match it will keep
              #searching.
              #I made some assumptions on what the possible values could be
              #So they may need to be tweked a bit.
              while($file =~ /N [0-9]+; [0-9]+;([a-fA-F0-9:]+);/g)  {
          
                  #Insert your output formating here.
                  print $1,"\n";
          
              }
          Yes I know nobody asked for a entire solution but I have to much fun doing this.
          Last edited by 25; 05-03-2007, 03:18 PM.

          Comment

          • Alex Franke
            Veteran Member
            • Feb 2007
            • 2641
            • Chapel Hill, NC
            • Ryobi BT3100

            #6
            radhak, I see the "Disable smilies" now, but alas, I don't think I had that option when I was trying to edit my original post... (I posted, then noticed the smilies, then tried to "edit" the post to get rid of them.) Thanks for the tip, though!

            Look carefully: there's an extra semicolon after the first eight byte pairs. Assuming you intended to highlight the last semicolon in blue, too, and there are always two sets of eight byte-pairs, then the original regex I posted will match, or you could modify 25's accordingly. If there are more than two sets (but always the same number), replace the last "2" with the number of sets to expect. If there's one or more, then use a "+" instead of the "{2}", just as user 25 does.

            BTW, \d is the same thing as 0-9, so the solutions are very similar indeed.

            Don't forget you can use split to parse out the byte pairs, and join to reformat them if you need to.

            And like redhak says, if you're looking for that specific byte sequence, then just search for that specific pattern and don't worry about all the fancy pattern matching stuff.

            Also, BTW -- user 25 -- I noticed that this was your 256th post. Coincidence? =)
            Last edited by Alex Franke; 05-03-2007, 06:46 PM.
            online at http://www.theFrankes.com
            while ( !( succeed = try() ) ) ;
            "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

            Comment

            • crokett
              The Full Monte
              • Jan 2003
              • 10627
              • Mebane, NC, USA.
              • Ryobi BT3000

              #7
              radhak, I am looking for a generic string.

              25, thanks. it took you what, 15 minutes? I am really really rusty so it'd take me a couple hrs and lots of books.

              It's not an entire solution yet - I still need to take the values I find with your search string and search other parts of the file then add the formatting and spit the new file out. but it is a really good start.

              That is why I was thinking of building an array - I can then traverse the array searching the file in turn and based on what I find do one of several things.

              The idea is to automate what took me a few hours of searching and organizing. I'd still need to do the analysis but it would be presented in a much more organized format for me.
              David

              The chief cause of failure in this life is giving up what you want most for what you want at the moment.

              Comment

              • Otter
                Senior Member
                • Nov 2003
                • 865
                • Cumming, GA, USA.
                • Delta Left Tilt UniSaw

                #8
                crokett

                I write in serveral programming enviroments, 2 of withch deal with the dissimlar and unknown, control systems. Can you provide a large sample and a clearer idea of dilimiter sets.

                Most of these operations can be scripted pretty quick. I love the this stuff, I don't want to take the chalenge out of the task for you, but it looks like fun.

                just want to play.
                Last edited by Otter; 05-03-2007, 08:47 PM. Reason: spelling witch I suck at
                All men dream, but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dream with open eyes, to make it possible

                T.E. Lawrence

                Comment

                • crokett
                  The Full Monte
                  • Jan 2003
                  • 10627
                  • Mebane, NC, USA.
                  • Ryobi BT3000

                  #9
                  Otter, here is a small sampling. names are changed to protect the innocent.

                  Here is where I think I will pull the initial info from. This shows exactly what is logged into the switch. This is the first few lines of 30 or so for this section of the file - but that will always vary. The blue/purple is the stuff I need to search for.

                  nsshow:
                  {
                  Type Pid COS PortName NodeName SCR
                  N 140400; 3;50:05:07:68:01:40:2e:1b;50:05:07:68:01:00:2e:1b; 3
                  FC4s: FCP [SOME_DEVICE]
                  Fabric Port Name: 20:04:00:05:1e:36:11:48



                  Once I get those values then I search in various spots in the file.

                  Here is one spot. Pink is the keyword in the file. Red is what I'd like in the output file, plus the blue values from above. I'd also like a check built in so anything listed in the above blue that is not in the below gets flagged. If the purples is in thelow it gets flagged. Once I get the code for the initial one I can reverse engineer for the other spots I need to check.,

                  Effective configuration:
                  cfg: SOME_CONFIG
                  zone: zone1
                  50:00:1f:e1:50:03:dd:4c
                  50:00:1f:e1:50:03:dd:49
                  21:00:00:e0:8b:1a:80:64
                  zone: zone2
                  50:00:1f:e1:50:03:dd:4c
                  50:00:1f:e1:50:03:dd:49
                  10:00:00:00:c9:3d:0f:df


                  Last edited by crokett; 05-04-2007, 05:25 AM.
                  David

                  The chief cause of failure in this life is giving up what you want most for what you want at the moment.

                  Comment

                  • 25
                    Established Member
                    • Jan 2004
                    • 294
                    • League City, Tx, USA.
                    • BT3100

                    #10
                    Originally posted by Alex Franke
                    Also, BTW -- user 25 -- I noticed that this was your 256th post. Coincidence? =)
                    I didn't even notice that, thats funny though.

                    Originally posted by crokett
                    25, thanks. it took you what, 15 minutes? I am really really rusty so it'd take me a couple hrs and lots of books.
                    Really not trying to brag but it was more like 5. It would of taken less but I had to look up how to slurp the file as a string since I usually do everything into an array .


                    This is still pretty dirty and I hope I understood you correctly on what was needed.

                    *DISCLAIMER: I didn't have much data to test on, it worked for what I had though, so you will need to test it more to make sure it works like you want.
                    Code:
                    #!/usr/bin/perl -w
                    
                        #Don't leave home without it.  
                        use strict;
                    
                        my $inFile  = "/tmp/test.dat"; 
                        my $cfgFile = "/tmp/cfgTest.dat";
                        my %config  = ();
                    
                        read_Config_Data($cfgFile, \%config);
                    
                        parse_Data_File($inFile, \%config);
                    
                    
                    ################################################################################
                    ###                             Parse Data File                              ###
                    ###                                                                          ###
                    ###  Purpose: Parses the data file and does stuff to it.                     ###
                    ###  Args:    File Name - Path to the config file.                           ###
                    ###           Config Hash - Hash that will contain all the config data.      ###
                    ###                                                                          ###
                    ###  Returns: Nothing.                                                       ###
                    ################################################################################
                    sub parse_Data_File  {
                        my $inFile  = $_[0];
                        my $cfgHash = $_[1];
                        my @file    = ();
                        my $line    = '';
                        my $zone    = '';
                        my $cfg     = '';
                    
                        if( read_File($inFile, \@file) )
                        {  die "Data file couldn't be found\n";  }
                    
                        for(my $i = 0; $i <= $#file; $i++)  {
                            $line   = $file[$i];
                    
                            chomp($line);
                    
                            if($line =~ /N [0-9]+; [0-9]+;([a-fA-F0-9:]+);([a-fA-F0-9:]+);/o)  {
                                my $firstSet    = lc($1);
                                my $secondSet   = lc($2);
                    
                                if( !exists($cfgHash->{$firstSet}) && !defined($cfgHash->{$firstSet}) )
                                {  printf("%-20s not found.\n", $firstSet);  }
                    
                                if( exists($cfgHash->{$secondSet}) && defined($cfgHash->{$secondSet}) )  {
                    
                                    printf("%-20s found in %-15s  in %-15s\n",
                                           $secondSet,
                                           $cfgHash->{$secondSet}[0],
                                           $cfgHash->{$secondSet}[1]
                                   );
                    
                                }
                    
                            }
                    
                        }
                    
                    }
                    
                    
                    ################################################################################
                    ###                             Read Config File                             ###
                    ###                                                                          ###
                    ###  Purpose: Setup the intial data from the config file.                    ###
                    ###  Args:    File Name - Path to the config file.                           ###
                    ###           Config Hash - Hash that will contain all the config data.      ###
                    ###                                                                          ###
                    ###  Returns: Nothing.                                                       ###
                    ################################################################################
                    sub read_Config_Data  {
                        my $inFile  = $_[0];
                        my $cfgHash = $_[1];
                        my @file    = ();
                        my $line    = '';
                        my $zone    = '';
                        my $cfg     = '';
                    
                        if( read_File($inFile, \@file) )
                        {  die "Config file couldn't be found\n";  }
                    
                        for(my $i = 0; $i <= $#file; $i++)  {
                            $line   = $file[$i];
                    
                            chomp($line);
                    
                            $line =~ s/\s+//go;
                    
                            if($line =~ /^zone:(.+)/io)  {
                                #Do you even care about the zone?
                                $zone = $1;
                            }
                            elsif($line =~ /^cfg:(.+)/io)  {
                                #Do you even care about the zone?
                                $cfg = $1;
                            }
                            elsif($line =~ /Effectiveconfiguration/io)  {
                                next;
                            }
                            elsif($line =~ /([a-fA-F0-9:]+)/o)  {
                                $cfgHash->{lc($1)} = [$cfg, $zone];
                            }
                    
                        }
                    
                    }
                    
                    
                    ################################################################################
                    ###                                Read File                                 ###
                    ###                                                                          ###
                    ###  Purpose: Reads in a file to the given ref to an array.                  ###
                    ###  Args:    Filename and path, reference to an array.                      ###
                    ###  Returns: 0 on failure and 1 on success.                                 ###
                    ################################################################################
                    sub read_File  {
                        my $inFile      = $_[0];
                        my $r_outFile   = $_[1];
                    
                        #Check that the input file exists
                        if( !defined($inFile) || !(-s $inFile) )  {
                        
                            warn 'Input File does not exist: ',"$inFile\n";
                    
                            return 1;
                    
                        }
                    
                        open(FH, '< '.$inFile) or die $@, '  ',$!;
                    
                        @{ $r_outFile } = <FH>;
                    
                        close(FH);
                    
                        return 0;
                    
                    }
                    Also any comments on the coding style are welcome and appreciated.

                    Comment

                    • crokett
                      The Full Monte
                      • Jan 2003
                      • 10627
                      • Mebane, NC, USA.
                      • Ryobi BT3000

                      #11
                      Thanks. I will test it in the next few days. right now I am in the middle of a customer down call. Happy Happy Joy Joy.
                      David

                      The chief cause of failure in this life is giving up what you want most for what you want at the moment.

                      Comment

                      Working...