?
Solved

Saving into multiple array?

Posted on 2003-10-27
3
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
jmcg earned 375 total points
ID: 9631760
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
ID: 10038312
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans
Suggested Courses

777 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