Solved

Saving into multiple array?

Posted on 2003-10-27
3
164 Views
Last Modified: 2010-03-05
I have this log, which updated frequently, every new log started with date and time yyyy-mm-dd hh:mm:ss

2003-09-12 11:42:14 TRAP, SNMP v1, community public
      SNMPv2-SMI::enterprises.20022 Enterprise Specific Trap (1) Uptime: 6 days, 16:42:28.00
      SNMPv2-SMI::enterprises.20022 = STRING: " 10000 | CRITICAL | 10.55.10.3 | TEST | STATUS | PCAUSE | TEST ONLY | "
2003-09-12 11:42:17  TRAP, SNMP v1, community public
      SNMPv2-SMI::enterprises.20022 Enterprise Specific Trap (1) Uptime: 6 days, 16:42:31.04
      SNMPv2-SMI::enterprises.20022 = STRING: " 10000 | CRITICAL | 10.55.10.3 | TEST | STATUS | PCAUSE | TEST ONLY | "
2003-09-12 11:42:18  TRAP, SNMP v1, community public
      SNMPv2-SMI::enterprises.20022 Enterprise Specific Trap (1) Uptime: 6 days, 16:42:32.02
      SNMPv2-SMI::enterprises.20022 = STRING: " 10000 | CRITICAL | 10.55.10.3 | TEST | STATUS | PCAUSE | TEST ONLY | "
2003-09-12 11:42:19  TRAP, SNMP v1, community public

I want to be able to parse it and save it into multiple array automatically, i have make some script to parse it with this script:

$trapfile = "trapd.log";      
$done = 0;            
      open(IN,"<$trapfile");
      while(<IN>)
      {
               chomp;
               if(/^\s*SNMPv2\x2dSMI\x3a\x3aenterprises/)
               {
                 $done = 1;
                 $prevlog = $log;
                 s/^\s*//g;
                 $log = $_;
               }
               else
               {
                   $log = $log.$_;
               }
               if($done)
               {
                   ($string) = ($prevlog =~ /STRING\x3a\s\x22(\d+.+\x7c)\x22/g);
                   print "STRING : $string\n" if($string);
                   $done = 0;
               }
      }

        ($string) = ($log =~ /\x22\s(\d+.+\x7c\s)\x22/g);
        @itemz=split(/\s\x7c\s/,$string);

print $itemz[0];
print $itemz[1];  
print $itemz[2];  
print $itemz[3];  
print $itemz[4];
print $itemz[5];  
print $itemz[6];

but i still confused to save it in multiple array, how can I do it in perl?
0
Comment
Question by:afht
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
jmcg earned 125 total points
Comment Utility
Conceptually, your parsing script looks reasonable enough. I don't understand why you felt it necessary to hexify various characters in your regular expressions (and I didn't check them). It's also not quite clear to me that you need to reassemble a multi-line string when the data you are interested in is already all on one line (or are the line breaks in the STRING = component present in the trapd.log file and not just an artifact of posting here?).

By "multiple array" do you possibly mean "multi-dimensioned array"? I'm afraid I cannot visualize the data structure you would like to produce from your parsed data.

You can certainly make a slight modification to your split line and add something to keep the data in a two-dimensional array:

  my @itemz = split /\s\x7c\s/, $string;
  push @saved_data, \@itemz;

The "my" is needed so you don't overwrite previously saved data each time through the loop.

0
 
LVL 20

Expert Comment

by:jmcg
Comment Utility
Nothing has happened on this question in over 2 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by jmcg [grade B] (progress towards, but not a complete solution).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now