Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Saving into multiple array?

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
afht
Asked:
afht
  • 2
1 Solution
 
jmcgOwnerCommented:
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
 
jmcgOwnerCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now