Solved

Saving into multiple array?

Posted on 2003-10-27
3
176 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

864 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

24 Experts available now in Live!

Get 1:1 Help Now