As others pointed out you haven't explained the context very clearly, however here's an example of how you can parse a logfile similar to what you show:
use strict;
use warnings;
use Data::Dumper;
sub readlogfile {
my ($filename) = @_;
open my $logfile, '<', $filename
or die "open $filename: $!";
my (@lookup, $entry, $trap);
while (<$logfile>) {
chomp;
if ( /^Date:\s+(.+)$/ ) {
push @lookup, ($entry = { DATE => $1 });
undef $trap;
} elsif ( !$entry ) {
next;
} elsif ( /^(SServer|Device|CString|Port):\s+(.+)$/ ) {
$entry->{uc $1} = $2;
} elsif ( /^\s+(Trap OID|Generic|Specific|Varbind|Type):\s+(.+
+)$/ ) {
$trap->{uc $1} = $2;
} elsif ( /^\s+Data:\s+(.+)$/ ) {
push @{$entry->{TRAPS}{TRAP_LIST}}, $trap if $trap;
$trap = { DATA => $1 };
}
}
return @lookup;
}
print Dumper readlogfile("logfile.txt");
I've rudely assumed "Trap OID", "Generic", and "Specific" actually belong in a trap rather than the entry. If that's not the case you can easily edit it ofcourse.
In any case I hope this is useful for solving your problem
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.